Skip to content

Commit

Permalink
Add crossorigin attribute back to link headers
Browse files Browse the repository at this point in the history
  • Loading branch information
andyexeter committed Apr 29, 2024
1 parent 7805879 commit 82af4b3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
class PreloadAssetsEventListener implements EventSubscriberInterface
{
public function __construct(
private EntrypointRenderer $entrypointRenderer
private EntrypointRenderer $entrypointRenderer,
private string|bool $crossOriginAttribute
) {
}

Expand All @@ -37,13 +38,22 @@ public function onKernelResponse(ResponseEvent $event): void
$rel = $tag->isModule() || $tag->isModulePreload() ? 'modulepreload' : 'preload';
$link = $this->createLink($rel, $href)->withAttribute('as', 'script');

if (is_string($this->crossOriginAttribute)) {
$link = $link->withAttribute('crossorigin', $this->crossOriginAttribute);
}

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

foreach ($this->entrypointRenderer->getRenderedStyles() as $filePath => $tag) {
$href = $tag->getAttribute('href');
if (is_string($href)) {
$link = $this->createLink('preload', $href)->withAttribute('as', 'style');

if (is_string($this->crossOriginAttribute)) {
$link = $link->withAttribute('crossorigin', $this->crossOriginAttribute);
}

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

0 comments on commit 82af4b3

Please sign in to comment.