Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing integrity hash on preload #161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Asset/TagRenderer.php
Expand Up @@ -28,6 +28,7 @@ class TagRenderer implements ResetInterface
private $eventDispatcher;

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

public function __construct(
$entrypointLookupCollection,
Expand Down Expand Up @@ -60,7 +61,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 @@ -91,13 +92,14 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
$this->convertArrayToAttributes($attributes)
);

$this->renderedFiles['scripts'][] = $attributes['src'];
$this->renderedFiles['scripts'][] = $attributes["src"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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 @@ -129,7 +131,8 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
$this->convertArrayToAttributes($attributes)
);

$this->renderedFiles['styles'][] = $attributes['href'];
$this->renderedFiles['styles'][] = $attributes["href"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->renderedFiles['styles'][] = $attributes["href"];
$this->renderedFiles['styles'][] = $attributes['href'];

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

return implode('', $scriptTags);
Expand All @@ -145,14 +148,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()
{
$this->renderedFiles = [
$this->renderedFiles = $this->renderedFilesWithAttributes = [
'scripts' => [],
'styles' => [],
];
Expand Down
26 changes: 18 additions & 8 deletions src/EventListener/PreLoadAssetsEventListener.php
Expand Up @@ -53,21 +53,31 @@ class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGe
$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
Expand Up @@ -299,8 +299,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
Expand Up @@ -28,8 +28,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 @@ -58,7 +67,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