Skip to content

Commit f5b5c97

Browse files
committed
Pass all attributes to Link header
Signed-off-by: Hugo Alliaume <[email protected]>
1 parent 707cd49 commit f5b5c97

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v2.2.0
4+
5+
- #236 Allow entrypoints.json to be hosted remotely (@rlvdx & @Kocal)
6+
- #232 fix: correctly wire the build-time file into kernel.build_dir (@dkarlovi)
7+
- #237 Fix missing integrity hash on preload (@arnaud-ritti & @Kocal)
8+
39
## v2.1.0
410

511
- #233 Add support for PHP 8.3 and PHP 8.4 (@Kocal)

src/Asset/TagRenderer.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class TagRenderer implements ResetInterface
2828
private $defaultLinkAttributes;
2929
private $eventDispatcher;
3030

31+
// TODO WebpackEncoreBundle 3.0: remove this property
3132
private $renderedFiles = [];
33+
// TODO WebpackEncoreBundle 3.0: rename this property to $renderedFiles
3234
private $renderedFilesWithAttributes = [];
3335

3436
public function __construct(
@@ -87,7 +89,7 @@ public function renderWebpackScriptTags(string $entryName, ?string $packageName
8789
return implode('', $scriptTags);
8890
}
8991

90-
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
92+
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
9193
{
9294
$entrypointName = $entrypointName ?: '_default';
9395
$scriptTags = [];
@@ -126,24 +128,26 @@ public function renderWebpackLinkTags(string $entryName, ?string $packageName =
126128
return implode('', $scriptTags);
127129
}
128130

129-
public function getRenderedScripts(): array
131+
/**
132+
* @param bool $includeAttributes Whether to include the attributes or not.
133+
* In WebpackEncoreBundle 3.0, this parameter will be removed,
134+
* and the attributes will always be included.
135+
* TODO WebpackEncoreBundle 3.0
136+
*/
137+
public function getRenderedScripts(bool $includeAttributes = false): array
130138
{
131-
return $this->renderedFiles['scripts'];
139+
return $includeAttributes ? $this->renderedFilesWithAttributes['scripts'] : $this->renderedFiles['scripts'];
132140
}
133141

134-
public function getRenderedStyles(): array
142+
/**
143+
* @param bool $includeAttributes Whether to include the attributes or not.
144+
* In WebpackEncoreBundle 3.0, this parameter will be removed,
145+
* and the attributes will always be included.
146+
* TODO WebpackEncoreBundle 3.0
147+
*/
148+
public function getRenderedStyles(bool $includeAttributes = false): array
135149
{
136-
return $this->renderedFiles['styles'];
137-
}
138-
139-
public function getRenderedScriptsWithAttributes(): array
140-
{
141-
return $this->renderedFilesWithAttributes['scripts'];
142-
}
143-
144-
public function getRenderedStylesWithAttributes(): array
145-
{
146-
return $this->renderedFilesWithAttributes['styles'];
150+
return $includeAttributes ? $this->renderedFilesWithAttributes['styles'] : $this->renderedFiles['styles'];
147151
}
148152

149153
public function getDefaultAttributes(): array

src/EventListener/PreLoadAssetsEventListener.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,31 @@ public function onKernelResponse(ResponseEvent $event): void
4747
/** @var GenericLinkProvider $linkProvider */
4848
$linkProvider = $request->attributes->get('_links');
4949
$defaultAttributes = $this->tagRenderer->getDefaultAttributes();
50-
$crossOrigin = $defaultAttributes['crossorigin'] ?? false;
5150

52-
foreach ($this->tagRenderer->getRenderedScriptsWithAttributes() as $attributes) {
53-
$attributes = array_merge($defaultAttributes, $attributes);
51+
foreach ($this->tagRenderer->getRenderedScripts(true) as $attributes) {
52+
$src = $attributes['src'];
53+
unset($attributes['src']);
54+
$attributes = [...$defaultAttributes, ...$attributes];
5455

55-
$link = $this->createLink('preload', $attributes['src'])->withAttribute('as', 'script');
56+
$link = $this->createLink('preload', $src)
57+
->withAttribute('as', 'script');
5658

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']);
59+
foreach ($attributes as $k => $v) {
60+
$link = $link->withAttribute($k, $v);
6261
}
6362

6463
$linkProvider = $linkProvider->withLink($link);
6564
}
6665

67-
foreach ($this->tagRenderer->getRenderedStylesWithAttributes() as $attributes) {
68-
$attributes = array_merge($defaultAttributes, $attributes);
66+
foreach ($this->tagRenderer->getRenderedStyles(true) as $attributes) {
67+
$href = $attributes['href'];
68+
unset($attributes['href']);
69+
$attributes = [...$defaultAttributes, ...$attributes];
6970

70-
$link = $this->createLink('preload', $attributes['href'])->withAttribute('as', 'style');
71+
$link = $this->createLink('preload', $href)->withAttribute('as', 'style');
7172

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']);
73+
foreach ($attributes as $k => $v) {
74+
$link = $link->withAttribute($k, $v);
7775
}
7876

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

tests/Asset/TagRendererTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,18 @@ public function testGetRenderedFilesAndReset()
308308
[
309309
'src' => 'http://localhost:8080/build/file2.js',
310310
],
311-
], $renderer->getRenderedScriptsWithAttributes());
311+
], $renderer->getRenderedScripts(true));
312312
$this->assertSame([
313313
[
314314
'rel' => 'stylesheet',
315315
'href' => 'http://localhost:8080/build/file1.css',
316316
],
317-
], $renderer->getRenderedStylesWithAttributes());
317+
], $renderer->getRenderedStyles(true));
318318

319319
$renderer->reset();
320320
$this->assertEmpty($renderer->getRenderedScripts());
321321
$this->assertEmpty($renderer->getRenderedStyles());
322-
$this->assertEmpty($renderer->getRenderedScriptsWithAttributes());
323-
$this->assertEmpty($renderer->getRenderedStylesWithAttributes());
322+
$this->assertEmpty($renderer->getRenderedScripts(true));
323+
$this->assertEmpty($renderer->getRenderedStyles(true));
324324
}
325325
}

tests/EventListener/PreLoadAssetsEventListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ 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([
33+
$tagRenderer->expects($this->once())->method('getRenderedScripts')->with(true)->willReturn([
3434
[
3535
'src' => '/file1.js',
3636
],
3737
]);
38-
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([
38+
$tagRenderer->expects($this->once())->method('getRenderedStyles')->with(true)->willReturn([
3939
[
4040
'rel' => 'stylesheet',
4141
'href' => '/css/file1.css',
@@ -62,7 +62,7 @@ public function testItPreloadsAssets()
6262

6363
$this->assertSame('/css/file1.css', $links[1]->getHref());
6464
$this->assertSame(['preload'], $links[1]->getRels());
65-
$this->assertSame(['as' => 'style', 'crossorigin' => 'anonymous'], $links[1]->getAttributes());
65+
$this->assertSame(['as' => 'style', 'crossorigin' => 'anonymous', 'rel' => 'stylesheet'], $links[1]->getAttributes());
6666
}
6767

6868
public function testItReusesExistingLinkProvider()

0 commit comments

Comments
 (0)