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 2 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
4 changes: 2 additions & 2 deletions src/Asset/TagRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function renderWebpackScriptTags(string $entryName, string $packageName =
$this->convertArrayToAttributes($attributes)
);

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

Choose a reason for hiding this comment

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

Unfortunately, this is a BC break, as it changes the structure of what's returned from getRenderedScripts() and getRenderedStyles().

Probably we need to add and maintain a second property - e.g. $this->renderedFilesWithAttributes(). Then, in getRenderedScripts() (and also for styles), we could add a new bool $includeAttributes = false argument. If that's true, then we return the renderedFilesWithAttributes.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @weaverryan , Sorry for the delay. Thanks for your comment.
I fixed the BC break according your recommendations, if you want to check again.

Copy link

Choose a reason for hiding this comment

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

@weaverryan @jrushlow This PR has been open for almost 2 years now, could either one spare some time to review it further and merge it? Thanks in advance.

}

return implode('', $scriptTags);
Expand Down Expand Up @@ -129,7 +129,7 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
$this->convertArrayToAttributes($attributes)
);

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

return implode('', $scriptTags);
Expand Down
26 changes: 18 additions & 8 deletions src/EventListener/PreLoadAssetsEventListener.php
Original file line number Diff line number Diff line change
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->getRenderedScripts() 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->getRenderedStyles() 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
16 changes: 14 additions & 2 deletions tests/Asset/TagRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,20 @@ public function testGetRenderedFilesAndReset()

$renderer->renderWebpackScriptTags('my_entry');
$renderer->renderWebpackLinkTags('my_entry');
$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->getRenderedScripts());
$this->assertSame([
[
'rel' => 'stylesheet',
'href' => 'http://localhost:8080/build/file1.css',
],
], $renderer->getRenderedStyles());

$renderer->reset();
$this->assertEmpty($renderer->getRenderedScripts());
Expand Down
19 changes: 16 additions & 3 deletions tests/EventListener/PreLoadAssetsEventListenerTest.php
Original file line number Diff line number Diff line change
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