Skip to content

Commit

Permalink
Fix missing integrity hash on preload
Browse files Browse the repository at this point in the history
Signed-off-by: Hugo Alliaume <[email protected]>
  • Loading branch information
arnaud-ritti authored and Kocal committed Oct 1, 2024
1 parent 16af8a3 commit 2025c41
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
23 changes: 18 additions & 5 deletions src/Asset/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TagRenderer implements ResetInterface
private $eventDispatcher;

private $renderedFiles = [];
private $renderedFilesWithAttributes = [];

public function __construct(
EntrypointLookupCollectionInterface $entrypointLookupCollection,
Expand All @@ -48,7 +49,7 @@ public function __construct(
$this->reset();
}

public function renderWebpackScriptTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
public function renderWebpackScriptTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
{
$entrypointName = $entrypointName ?: '_default';
$scriptTags = [];
Expand Down Expand Up @@ -79,13 +80,14 @@ public function renderWebpackScriptTags(string $entryName, ?string $packageName
$this->convertArrayToAttributes($attributes)
);

$this->renderedFiles['scripts'][] = $attributes['src'];
$this->renderedFiles['scripts'][] = $attributes["src"];
$this->renderedFilesWithAttributes['scripts'][] = $attributes;
}

return implode('', $scriptTags);
}

public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = []): string
public function renderWebpackLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraAttributes = [], bool $includeAttributes = false): string
{
$entrypointName = $entrypointName ?: '_default';
$scriptTags = [];
Expand Down Expand Up @@ -117,7 +119,8 @@ public function renderWebpackLinkTags(string $entryName, ?string $packageName =
$this->convertArrayToAttributes($attributes)
);

$this->renderedFiles['styles'][] = $attributes['href'];
$this->renderedFiles['styles'][] = $attributes["href"];
$this->renderedFilesWithAttributes['styles'][] = $attributes;
}

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

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

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

public function getDefaultAttributes(): array
{
return $this->defaultAttributes;
}

public function reset(): void
{
$this->renderedFiles = [
$this->renderedFiles = $this->renderedFilesWithAttributes = [
'scripts' => [],
'styles' => [],
];
Expand Down
26 changes: 18 additions & 8 deletions src/EventListener/PreLoadAssetsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,31 @@ public function onKernelResponse(ResponseEvent $event): void
$defaultAttributes = $this->tagRenderer->getDefaultAttributes();
$crossOrigin = $defaultAttributes['crossorigin'] ?? false;

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

if (false !== $crossOrigin) {
$link = $link->withAttribute('crossorigin', $crossOrigin);
$link = ($this->createLink('preload', $attributes['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']);
}

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

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

if (false !== $crossOrigin) {
$link = $link->withAttribute('crossorigin', $crossOrigin);
$link = ($this->createLink('preload', $attributes['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']);
}

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

$this->assertSame([
[
'src' => 'http://localhost:8080/build/file1.js',
],
[
'src' => 'http://localhost:8080/build/file2.js',
],
], $renderer->getRenderedScriptsWithAttributes());
$this->assertSame([
[
'rel' => 'stylesheet',
'href' => 'http://localhost:8080/build/file1.css',
],
], $renderer->getRenderedStylesWithAttributes());


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

$request = new Request();
$response = new Response();
Expand Down Expand Up @@ -60,7 +69,11 @@ public function testItReusesExistingLinkProvider()
{
$tagRenderer = $this->createMock(TagRenderer::class);
$tagRenderer->expects($this->once())->method('getDefaultAttributes')->willReturn(['crossorigin' => 'anonymous']);
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn(['/file1.js']);
$tagRenderer->expects($this->once())->method('getRenderedScripts')->willReturn([
[
'src' => '/file1.js',
],
]);
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([]);

$request = new Request();
Expand Down

0 comments on commit 2025c41

Please sign in to comment.