Skip to content

Commit 51aa24e

Browse files
committed
ACP2E-4128: [CLOUD] Getting 404 for JS file on checkout page on first attempt after implementing sri patch
1 parent 49ef17c commit 51aa24e

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

app/code/Magento/Csp/Plugin/GenerateAssetIntegrity.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,8 @@ public function afterCreateRequireJsMixinsAsset(
103103
FileManager $subject,
104104
File $result
105105
): File {
106-
if (PHP_SAPI == 'cli') {
107-
if (in_array($result->getContentType(), self::CONTENT_TYPES)) {
108-
try {
109-
$content = $result->getContent();
110-
} catch (\Exception $e) {
111-
$content = null;
112-
}
113-
$path = $result->getPath();
114-
115-
if ($content !== null) {
116-
$integrity = $this->integrityFactory->create(
117-
[
118-
"data" => [
119-
'hash' => $this->hashGenerator->generate($content),
120-
'path' => $path
121-
]
122-
]
123-
);
124-
$this->integrityCollector->collect($integrity);
125-
}
126-
}
106+
if (PHP_SAPI === 'cli') {
107+
$this->generateHash($result);
127108
}
128109

129110
return $result;
@@ -133,39 +114,50 @@ public function afterCreateRequireJsMixinsAsset(
133114
* Generates integrity for static JS asset.
134115
*
135116
* @param FileManager $subject
136-
* @param File $result
117+
* @param File|false $result
137118
*
138119
* @return File
139120
*
140121
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
141122
*/
142123
public function afterCreateStaticJsAsset(
143124
FileManager $subject,
144-
File $result
125+
File|false $result
145126
): File {
146-
if (PHP_SAPI == 'cli') {
147-
if (in_array($result->getContentType(), self::CONTENT_TYPES)) {
148-
try {
149-
$content = $result->getContent();
150-
} catch (\Exception $e) {
151-
$content = null;
152-
}
153-
$path = $result->getPath();
154-
155-
if ($content !== null) {
156-
$integrity = $this->integrityFactory->create(
157-
[
158-
"data" => [
159-
'hash' => $this->hashGenerator->generate($content),
160-
'path' => $path
161-
]
162-
]
163-
);
164-
$this->integrityCollector->collect($integrity);
165-
}
166-
}
127+
if ($result !== false && PHP_SAPI === 'cli') {
128+
$this->generateHash($result);
167129
}
168130

169131
return $result;
170132
}
133+
134+
/**
135+
* Generates hash for the given file result if it matches the supported content types.
136+
*
137+
* @param File $result
138+
* @return void
139+
*/
140+
private function generateHash(File $result): void
141+
{
142+
if (in_array($result->getContentType(), self::CONTENT_TYPES)) {
143+
try {
144+
$content = $result->getContent();
145+
} catch (\Exception $e) {
146+
$content = null;
147+
}
148+
$path = $result->getPath();
149+
150+
if ($content !== null) {
151+
$integrity = $this->integrityFactory->create(
152+
[
153+
"data" => [
154+
'hash' => $this->hashGenerator->generate($content),
155+
'path' => $path
156+
]
157+
]
158+
);
159+
$this->integrityCollector->collect($integrity);
160+
}
161+
}
162+
}
171163
}

0 commit comments

Comments
 (0)