Skip to content

Commit

Permalink
Pass all attributes to Link header
Browse files Browse the repository at this point in the history
Signed-off-by: Hugo Alliaume <[email protected]>
  • Loading branch information
Kocal committed Oct 1, 2024
1 parent 707cd49 commit d682c35
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v2.2.0

- #236 Allow entrypoints.json to be hosted remotely (@rlvdx & @Kocal)
- #232 fix: correctly wire the build-time file into kernel.build_dir (@dkarlovi)
- #237 Fix missing integrity hash on preload (@arnaud-ritti & @Kocal)

## v2.1.0

- #233 Add support for PHP 8.3 and PHP 8.4 (@Kocal)
Expand Down
36 changes: 21 additions & 15 deletions src/Asset/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class TagRenderer implements ResetInterface
private $defaultLinkAttributes;
private $eventDispatcher;

// TODO WebpackEncoreBundle 3.0: remove this property
private $renderedFiles = [];
// TODO WebpackEncoreBundle 3.0: rename this property to $renderedFiles
private $renderedFilesWithAttributes = [];

public function __construct(
Expand Down Expand Up @@ -87,7 +89,7 @@ public function renderWebpackScriptTags(string $entryName, ?string $packageName
return implode('', $scriptTags);
}

public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
{
$entrypointName = $entrypointName ?: '_default';
$scriptTags = [];
Expand Down Expand Up @@ -126,24 +128,28 @@ public function renderWebpackLinkTags(string $entryName, ?string $packageName =
return implode('', $scriptTags);
}

public function getRenderedScripts(): array
/**
* @param bool $includeAttributes Whether to include the attributes or not.
* In WebpackEncoreBundle 3.0, this parameter will be removed,
* and the attributes will always be included.
* TODO WebpackEncoreBundle 3.0
* @return ($includeAttributes is true ? list<array<string, mixed>> : list<string>)
*/
public function getRenderedScripts(bool $includeAttributes = false): array
{
return $this->renderedFiles['scripts'];
return $includeAttributes ? $this->renderedFilesWithAttributes['scripts'] : $this->renderedFiles['scripts'];
}

public function getRenderedStyles(): array
/**
* @param bool $includeAttributes Whether to include the attributes or not.
* In WebpackEncoreBundle 3.0, this parameter will be removed,
* and the attributes will always be included.
* TODO WebpackEncoreBundle 3.0
* @return ($includeAttributes is true ? list<array<string, mixed>> : list<string>)
*/
public function getRenderedStyles(bool $includeAttributes = false): array
{
return $this->renderedFiles['styles'];
}

public function getRenderedScriptsWithAttributes(): array
{
return $this->renderedFilesWithAttributes['scripts'];
}

public function getRenderedStylesWithAttributes(): array
{
return $this->renderedFilesWithAttributes['styles'];
return $includeAttributes ? $this->renderedFilesWithAttributes['styles'] : $this->renderedFiles['styles'];
}

public function getDefaultAttributes(): array
Expand Down
32 changes: 15 additions & 17 deletions src/EventListener/PreLoadAssetsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,31 @@ public function onKernelResponse(ResponseEvent $event): void
/** @var GenericLinkProvider $linkProvider */
$linkProvider = $request->attributes->get('_links');
$defaultAttributes = $this->tagRenderer->getDefaultAttributes();
$crossOrigin = $defaultAttributes['crossorigin'] ?? false;

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

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

if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) {
$link = $link->withAttribute('crossorigin', $attributes['crossorigin']);
}
if (!empty($attributes['integrity'])) {
$link = $link->withAttribute('integrity', $attributes['integrity']);
foreach ($attributes as $k => $v) {
$link = $link->withAttribute($k, $v);
}

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

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

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

if (!empty($attributes['crossorigin']) && false !== $attributes['crossorigin']) {
$link = $link->withAttribute('crossorigin', $attributes['crossorigin']);
}
if (!empty($attributes['integrity'])) {
$link = $link->withAttribute('integrity', $attributes['integrity']);
foreach ($attributes as $k => $v) {
$link = $link->withAttribute($k, $v);
}

$linkProvider = $linkProvider->withLink($link);
Expand Down
8 changes: 4 additions & 4 deletions tests/Asset/TagRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,18 @@ public function testGetRenderedFilesAndReset()
[
'src' => 'http://localhost:8080/build/file2.js',
],
], $renderer->getRenderedScriptsWithAttributes());
], $renderer->getRenderedScripts(true));
$this->assertSame([
[
'rel' => 'stylesheet',
'href' => 'http://localhost:8080/build/file1.css',
],
], $renderer->getRenderedStylesWithAttributes());
], $renderer->getRenderedStyles(true));

$renderer->reset();
$this->assertEmpty($renderer->getRenderedScripts());
$this->assertEmpty($renderer->getRenderedStyles());
$this->assertEmpty($renderer->getRenderedScriptsWithAttributes());
$this->assertEmpty($renderer->getRenderedStylesWithAttributes());
$this->assertEmpty($renderer->getRenderedScripts(true));
$this->assertEmpty($renderer->getRenderedStyles(true));
}
}
6 changes: 3 additions & 3 deletions tests/EventListener/PreLoadAssetsEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function testItPreloadsAssets()
{
$tagRenderer = $this->createMock(TagRenderer::class);
$tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']);
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([
$tagRenderer->expects($this->once())->method('getRenderedScripts')->with(true)->willReturn([
[
'src' => '/file1.js',
],
]);
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([
$tagRenderer->expects($this->once())->method('getRenderedStyles')->with(true)->willReturn([
[
'rel' => 'stylesheet',
'href' => '/css/file1.css',
Expand All @@ -62,7 +62,7 @@ public function testItPreloadsAssets()

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

public function testItReusesExistingLinkProvider()
Expand Down

0 comments on commit d682c35

Please sign in to comment.