diff --git a/src/Asset/TagRenderer.php b/src/Asset/TagRenderer.php index d84617eb..d11aa995 100644 --- a/src/Asset/TagRenderer.php +++ b/src/Asset/TagRenderer.php @@ -28,6 +28,7 @@ class TagRenderer implements ResetInterface private $eventDispatcher; private $renderedFiles = []; + private $renderedFilesWithAttributes = []; public function __construct( $entrypointLookupCollection, @@ -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 = []; @@ -91,13 +92,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 = []; @@ -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"]; + $this->renderedFilesWithAttributes['styles'][] = $attributes; } return implode('', $scriptTags); @@ -145,6 +148,16 @@ 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; @@ -152,7 +165,7 @@ public function getDefaultAttributes(): array public function reset() { - $this->renderedFiles = [ + $this->renderedFiles = $this->renderedFilesWithAttributes = [ 'scripts' => [], 'styles' => [], ]; diff --git a/src/EventListener/PreLoadAssetsEventListener.php b/src/EventListener/PreLoadAssetsEventListener.php index 442440f2..55473367 100644 --- a/src/EventListener/PreLoadAssetsEventListener.php +++ b/src/EventListener/PreLoadAssetsEventListener.php @@ -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); diff --git a/tests/Asset/TagRendererTest.php b/tests/Asset/TagRendererTest.php index 6b57ac51..65955c9f 100644 --- a/tests/Asset/TagRendererTest.php +++ b/tests/Asset/TagRendererTest.php @@ -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()); } } diff --git a/tests/EventListener/PreLoadAssetsEventListenerTest.php b/tests/EventListener/PreLoadAssetsEventListenerTest.php index 696bb607..c7cc7a97 100644 --- a/tests/EventListener/PreLoadAssetsEventListenerTest.php +++ b/tests/EventListener/PreLoadAssetsEventListenerTest.php @@ -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(); @@ -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();