From 59d724b7f10dd8f25ca90a3d09c7a4c81330688a Mon Sep 17 00:00:00 2001 From: andyexeter Date: Fri, 9 Feb 2024 12:55:25 +0000 Subject: [PATCH] Update PreloadAssetsEventListener to use crossorigin bundle config option value --- .../src/DependencyInjection/PentatrionViteExtension.php | 1 + .../src/EventListener/PreloadAssetsEventListener.php | 6 ++++-- src/vite-bundle/src/Resources/config/services.yaml | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/vite-bundle/src/DependencyInjection/PentatrionViteExtension.php b/src/vite-bundle/src/DependencyInjection/PentatrionViteExtension.php index cacaedc5..b2915c04 100644 --- a/src/vite-bundle/src/DependencyInjection/PentatrionViteExtension.php +++ b/src/vite-bundle/src/DependencyInjection/PentatrionViteExtension.php @@ -43,6 +43,7 @@ public function load(array $bundleConfigs, ContainerBuilder $container): void $container->setParameter('pentatrion_vite.absolute_url', $bundleConfig['absolute_url']); $container->setParameter('pentatrion_vite.proxy_origin', $bundleConfig['proxy_origin']); $container->setParameter('pentatrion_vite.throw_on_missing_entry', $bundleConfig['throw_on_missing_entry']); + $container->setParameter('pentatrion_vite.crossorigin', $bundleConfig['crossorigin']); if ( count($bundleConfig['configs']) > 0) { diff --git a/src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php b/src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php index 4d6ca3ed..01aa980a 100644 --- a/src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php +++ b/src/vite-bundle/src/EventListener/PreloadAssetsEventListener.php @@ -11,10 +11,12 @@ class PreloadAssetsEventListener implements EventSubscriberInterface { private EntrypointRenderer $entrypointRenderer; + private string|bool $crossOriginAttribute; - public function __construct(EntrypointRenderer $entrypointRenderer) + public function __construct(EntrypointRenderer $entrypointRenderer, string|bool $crossOriginAttribute) { $this->entrypointRenderer = $entrypointRenderer; + $this->crossOriginAttribute = $crossOriginAttribute; } public function onKernelResponse(ResponseEvent $event): void @@ -39,7 +41,7 @@ public function onKernelResponse(ResponseEvent $event): void $link = $this->createLink('preload', $href)->withAttribute('as', 'script'); if ('module' === $tag->getAttribute('type')) { - $link = $link->withAttribute('crossorigin', true); + $link = $link->withAttribute('crossorigin', $this->crossOriginAttribute ?: 'anonymous'); } $linkProvider = $linkProvider->withLink($link); diff --git a/src/vite-bundle/src/Resources/config/services.yaml b/src/vite-bundle/src/Resources/config/services.yaml index df94be8a..518d6a07 100644 --- a/src/vite-bundle/src/Resources/config/services.yaml +++ b/src/vite-bundle/src/Resources/config/services.yaml @@ -56,6 +56,7 @@ services: tags: ["kernel.event_subscriber"] arguments: - "@pentatrion_vite.entrypoint_renderer" + - "%pentatrion_vite.crossorigin%" pentatrion_vite.file_accessor: class: Pentatrion\ViteBundle\Service\FileAccessor