Skip to content

Commit 2025c41

Browse files
Arnaud RITTIKocal
authored andcommitted
Fix missing integrity hash on preload
Signed-off-by: Hugo Alliaume <hugo@alliau.me>
1 parent 16af8a3 commit 2025c41

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

src/Asset/TagRenderer.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TagRenderer implements ResetInterface
2929
private $eventDispatcher;
3030

3131
private $renderedFiles = [];
32+
private $renderedFilesWithAttributes = [];
3233

3334
public function __construct(
3435
EntrypointLookupCollectionInterface $entrypointLookupCollection,
@@ -48,7 +49,7 @@ public function __construct(
4849
$this->reset();
4950
}
5051

51-
public function renderWebpackScriptTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
52+
public function renderWebpackScriptTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
5253
{
5354
$entrypointName = $entrypointName ?: '_default';
5455
$scriptTags = [];
@@ -79,13 +80,14 @@ public function renderWebpackScriptTags(string $entryName, ?string $packageName
7980
$this->convertArrayToAttributes($attributes)
8081
);
8182

82-
$this->renderedFiles['scripts'][] = $attributes['src'];
83+
$this->renderedFiles['scripts'][] = $attributes["src"];
84+
$this->renderedFilesWithAttributes['scripts'][] = $attributes;
8385
}
8486

8587
return implode('', $scriptTags);
8688
}
8789

88-
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
90+
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
8991
{
9092
$entrypointName = $entrypointName ?: '_default';
9193
$scriptTags = [];
@@ -117,7 +119,8 @@ public function renderWebpackLinkTags(string $entryName, ?string $packageName =
117119
$this->convertArrayToAttributes($attributes)
118120
);
119121

120-
$this->renderedFiles['styles'][] = $attributes['href'];
122+
$this->renderedFiles['styles'][] = $attributes["href"];
123+
$this->renderedFilesWithAttributes['styles'][] = $attributes;
121124
}
122125

123126
return implode('', $scriptTags);
@@ -133,14 +136,24 @@ public function getRenderedStyles(): array
133136
return $this->renderedFiles['styles'];
134137
}
135138

139+
public function getRenderedScriptsWithAttributes(): array
140+
{
141+
return $this->renderedFilesWithAttributes['scripts'];
142+
}
143+
144+
public function getRenderedStylesWithAttributes(): array
145+
{
146+
return $this->renderedFilesWithAttributes['styles'];
147+
}
148+
136149
public function getDefaultAttributes(): array
137150
{
138151
return $this->defaultAttributes;
139152
}
140153

141154
public function reset(): void
142155
{
143-
$this->renderedFiles = [
156+
$this->renderedFiles = $this->renderedFilesWithAttributes = [
144157
'scripts' => [],
145158
'styles' => [],
146159
];

src/EventListener/PreLoadAssetsEventListener.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,31 @@ public function onKernelResponse(ResponseEvent $event): void
4949
$defaultAttributes = $this->tagRenderer->getDefaultAttributes();
5050
$crossOrigin = $defaultAttributes['crossorigin'] ?? false;
5151

52-
foreach ($this->tagRenderer->getRenderedScripts() as $href) {
53-
$link = $this->createLink('preload', $href)->withAttribute('as', 'script');
52+
foreach ($this->tagRenderer->getRenderedScriptsWithAttributes() as $attributes) {
53+
$attributes = array_merge($defaultAttributes, $attributes);
5454

55-
if (false !== $crossOrigin) {
56-
$link = $link->withAttribute('crossorigin', $crossOrigin);
55+
$link = ($this->createLink('preload', $attributes['src']))->withAttribute('as', 'script');
56+
57+
if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) {
58+
$link = $link->withAttribute('crossorigin', $attributes['crossorigin']);
59+
}
60+
if (!empty($attributes['integrity'])) {
61+
$link = $link->withAttribute('integrity', $attributes['integrity']);
5762
}
5863

5964
$linkProvider = $linkProvider->withLink($link);
6065
}
6166

62-
foreach ($this->tagRenderer->getRenderedStyles() as $href) {
63-
$link = $this->createLink('preload', $href)->withAttribute('as', 'style');
67+
foreach ($this->tagRenderer->getRenderedStylesWithAttributes() as $attributes) {
68+
$attributes = array_merge($defaultAttributes, $attributes);
6469

65-
if (false !== $crossOrigin) {
66-
$link = $link->withAttribute('crossorigin', $crossOrigin);
70+
$link = ($this->createLink('preload', $attributes['href']))->withAttribute('as', 'style');
71+
72+
if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) {
73+
$link = $link->withAttribute('crossorigin', $attributes['crossorigin']);
74+
}
75+
if (!empty($attributes['integrity'])) {
76+
$link = $link->withAttribute('integrity', $attributes['integrity']);
6777
}
6878

6979
$linkProvider = $linkProvider->withLink($link);

tests/Asset/TagRendererTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,26 @@ public function testGetRenderedFilesAndReset()
301301
$this->assertSame(['http://localhost:8080/build/file1.js', 'http://localhost:8080/build/file2.js'], $renderer->getRenderedScripts());
302302
$this->assertSame(['http://localhost:8080/build/file1.css'], $renderer->getRenderedStyles());
303303

304+
$this->assertSame([
305+
[
306+
'src' => 'http://localhost:8080/build/file1.js',
307+
],
308+
[
309+
'src' => 'http://localhost:8080/build/file2.js',
310+
],
311+
], $renderer->getRenderedScriptsWithAttributes());
312+
$this->assertSame([
313+
[
314+
'rel' => 'stylesheet',
315+
'href' => 'http://localhost:8080/build/file1.css',
316+
],
317+
], $renderer->getRenderedStylesWithAttributes());
318+
319+
304320
$renderer->reset();
305321
$this->assertEmpty($renderer->getRenderedScripts());
306322
$this->assertEmpty($renderer->getRenderedStyles());
323+
$this->assertEmpty($renderer->getRenderedScriptsWithAttributes());
324+
$this->assertEmpty($renderer->getRenderedStylesWithAttributes());
307325
}
308326
}

tests/EventListener/PreLoadAssetsEventListenerTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ public function testItPreloadsAssets()
3030
{
3131
$tagRenderer = $this->createMock(TagRenderer::class);
3232
$tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']);
33-
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn(['/file1.js']);
34-
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn(['/css/file1.css']);
33+
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([
34+
[
35+
'src' => '/file1.js',
36+
],
37+
]);
38+
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([
39+
[
40+
'rel' => 'stylesheet',
41+
'href' => '/css/file1.css',
42+
],
43+
]);
3544

3645
$request = new Request();
3746
$response = new Response();
@@ -60,7 +69,11 @@ public function testItReusesExistingLinkProvider()
6069
{
6170
$tagRenderer = $this->createMock(TagRenderer::class);
6271
$tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']);
63-
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn(['/file1.js']);
72+
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([
73+
[
74+
'src' => '/file1.js',
75+
],
76+
]);
6477
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([]);
6578

6679
$request = new Request();

0 commit comments

Comments
 (0)