@@ -31,6 +31,9 @@ class ImportMapRenderer
3131 private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL =
'https://ga.jspm.io/npm:[email protected] /dist/es-module-shims.js ' ;
3232 private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY = 'sha384-ie1x72Xck445i0j4SlNJ5W5iGeL3Dpa0zD48MZopgWsjNB/lt60SuG1iduZGNnJn ' ;
3333
34+ private const LOADER_JSON = "export default (async()=>await(await fetch('%s')).json())() " ;
35+ private const LOADER_CSS = "document.head.appendChild(Object.assign(document.createElement('link'),{rel:'stylesheet',href:'%s'})) " ;
36+
3437 public function __construct (
3538 private readonly ImportMapGenerator $ importMapGenerator ,
3639 private readonly ?Packages $ assetPackages = null ,
@@ -48,7 +51,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
4851 $ importMapData = $ this ->importMapGenerator ->getImportMapData ($ entryPoint );
4952 $ importMap = [];
5053 $ modulePreloads = [];
51- $ cssLinks = [];
54+ $ webLinks = [];
5255 $ polyfillPath = null ;
5356 foreach ($ importMapData as $ importName => $ data ) {
5457 $ path = $ data ['path ' ];
@@ -70,29 +73,34 @@ public function render(string|array $entryPoint, array $attributes = []): string
7073 }
7174
7275 $ preload = $ data ['preload ' ] ?? false ;
73- if ('css ' !== $ data ['type ' ]) {
76+ if ('json ' === $ data ['type ' ]) {
77+ $ importMap [$ importName ] = 'data:application/javascript, ' .str_replace ('% ' , '%25 ' , \sprintf (self ::LOADER_JSON , addslashes ($ path )));
78+ if ($ preload ) {
79+ $ webLinks [$ path ] = 'fetch ' ;
80+ }
81+ } elseif ('css ' !== $ data ['type ' ]) {
7482 $ importMap [$ importName ] = $ path ;
7583 if ($ preload ) {
7684 $ modulePreloads [] = $ path ;
7785 }
7886 } elseif ($ preload ) {
79- $ cssLinks [ ] = $ path ;
87+ $ webLinks [ $ path ] = ' style ' ;
8088 // importmap entry is a noop
8189 $ importMap [$ importName ] = 'data:application/javascript, ' ;
8290 } else {
83- $ importMap [$ importName ] = 'data:application/javascript, ' .rawurlencode ( \sprintf (' document.head.appendChild(Object.assign(document.createElement("link"),{rel:"stylesheet",href:"%s"})) ' , addslashes ($ path )));
91+ $ importMap [$ importName ] = 'data:application/javascript, ' .str_replace ( ' % ' , ' %25 ' , \sprintf (self :: LOADER_CSS , addslashes ($ path )));
8492 }
8593 }
8694
8795 $ output = '' ;
88- foreach ($ cssLinks as $ url ) {
89- $ url = $ this -> escapeAttributeValue ( $ url );
90-
91- $ output .= "\n <link rel= \" stylesheet \" href= \" $ url \" > " ;
96+ foreach ($ webLinks as $ url => $ as ) {
97+ if ( ' style ' === $ as ) {
98+ $ output .= "\n <link rel= \" stylesheet \" href= \"{ $ this -> escapeAttributeValue ( $ url )}\" > " ;
99+ }
92100 }
93101
94102 if (class_exists (AddLinkHeaderListener::class) && $ request = $ this ->requestStack ?->getCurrentRequest()) {
95- $ this ->addWebLinkPreloads ($ request , $ cssLinks );
103+ $ this ->addWebLinkPreloads ($ request , $ webLinks );
96104 }
97105
98106 $ scriptAttributes = $ attributes || $ this ->scriptAttributes ? ' ' .$ this ->createAttributesString ($ attributes ) : '' ;
@@ -186,12 +194,17 @@ private function createAttributesString(array $attributes, string $pattern = '%s
186194 return $ attributeString ;
187195 }
188196
189- private function addWebLinkPreloads (Request $ request , array $ cssLinks ): void
197+ private function addWebLinkPreloads (Request $ request , array $ links ): void
190198 {
191- $ cssPreloadLinks = array_map (fn ($ url ) => (new Link ('preload ' , $ url ))->withAttribute ('as ' , 'style ' ), $ cssLinks );
199+ foreach ($ links as $ url => $ as ) {
200+ $ links [$ url ] = (new Link ('preload ' , $ url ))->withAttribute ('as ' , $ as );
201+ if ('fetch ' === $ as ) {
202+ $ links [$ url ] = $ links [$ url ]->withAttribute ('crossorigin ' , 'anonymous ' );
203+ }
204+ }
192205
193206 if (null === $ linkProvider = $ request ->attributes ->get ('_links ' )) {
194- $ request ->attributes ->set ('_links ' , new GenericLinkProvider ($ cssPreloadLinks ));
207+ $ request ->attributes ->set ('_links ' , new GenericLinkProvider ($ links ));
195208
196209 return ;
197210 }
@@ -200,7 +213,7 @@ private function addWebLinkPreloads(Request $request, array $cssLinks): void
200213 return ;
201214 }
202215
203- foreach ($ cssPreloadLinks as $ link ) {
216+ foreach ($ links as $ link ) {
204217 $ linkProvider = $ linkProvider ->withLink ($ link );
205218 }
206219
0 commit comments