@@ -68,28 +68,12 @@ export const includePreloader = (
68
68
`document.head.appendChild(e)` +
69
69
`});`
70
70
: '' ;
71
- const opts : string [ ] = [ ] ;
72
- if ( debug ) {
73
- opts . push ( 'd:1' ) ;
74
- }
75
- if ( maxIdlePreloads ) {
76
- opts . push ( `P:${ maxIdlePreloads } ` ) ;
77
- }
78
- if ( preloadProbability ) {
79
- opts . push ( `Q:${ preloadProbability } ` ) ;
80
- }
81
- const optsStr = opts . length ? `,{${ opts . join ( ',' ) } }` : '' ;
82
71
// We are super careful not to interfere with the page loading.
83
72
const script =
84
- // First we wait for the onload event
85
- `let b=fetch("${ base } q-bundle-graph-${ manifestHash } .json");` +
86
73
insertLinks +
74
+ // First we wait for the onload event
87
75
`window.addEventListener('load',f=>{` +
88
- `f=_=>{` +
89
- `import("${ base } ${ preloadChunk } ").then(({l,p})=>{` +
90
- `l(${ JSON . stringify ( base ) } ,b${ optsStr } );` +
91
- `p(${ JSON . stringify ( referencedBundles ) } );` +
92
- `})};` +
76
+ `f=_=>import("${ base } ${ preloadChunk } ").then(({p})=>p(${ JSON . stringify ( referencedBundles ) } ));` +
93
77
// then we ask for idle callback
94
78
`try{requestIdleCallback(f,{timeout:2000})}` +
95
79
// some browsers don't support requestIdleCallback
@@ -124,8 +108,9 @@ export const includePreloader = (
124
108
export const preloaderPre = (
125
109
base : string ,
126
110
resolvedManifest : ResolvedManifest | undefined ,
127
- options : PreloaderOptions | boolean | undefined ,
128
- beforeContent : JSXNode < string > [ ]
111
+ options : PreloaderOptions | false | undefined ,
112
+ beforeContent : JSXNode < string > [ ] ,
113
+ nonce ?: string
129
114
) => {
130
115
const preloadChunk = resolvedManifest ?. manifest ?. preloader ;
131
116
if ( preloadChunk && options !== false ) {
@@ -140,16 +125,48 @@ export const preloaderPre = (
140
125
: undefined ;
141
126
initPreloader ( bundleGraph , preloaderOpts ) ;
142
127
}
143
- const core = resolvedManifest ?. manifest . core ;
128
+
129
+ const opts : string [ ] = [ ] ;
130
+ if ( options ?. debug ) {
131
+ opts . push ( 'd:1' ) ;
132
+ }
133
+ if ( options ?. maxIdlePreloads ) {
134
+ opts . push ( `P:${ options . maxIdlePreloads } ` ) ;
135
+ }
136
+ if ( options ?. preloadProbability ) {
137
+ opts . push ( `Q:${ options . preloadProbability } ` ) ;
138
+ }
139
+ const optsStr = opts . length ? `,{${ opts . join ( ',' ) } }` : '' ;
140
+
141
+ const hash = resolvedManifest ?. manifest . manifestHash ;
142
+
143
+ const script =
144
+ `let b=fetch("${ base } q-bundle-graph-${ hash } .json");` +
145
+ `import("${ base } ${ preloadChunk } ").then(({l})=>` +
146
+ `l(${ JSON . stringify ( base ) } ,b${ optsStr } )` +
147
+ `);` ;
148
+
144
149
beforeContent . push (
150
+ /**
151
+ * We add modulepreloads even when the script is at the top because they already fire during
152
+ * html download
153
+ */
145
154
jsx ( 'link' , { rel : 'modulepreload' , href : `${ base } ${ preloadChunk } ` } ) ,
146
155
jsx ( 'link' , {
147
156
rel : 'preload' ,
148
157
href : `${ base } q-bundle-graph-${ resolvedManifest ?. manifest . manifestHash } .json` ,
149
158
as : 'fetch' ,
150
159
crossorigin : 'anonymous' ,
160
+ } ) ,
161
+ jsx ( 'script' , {
162
+ type : 'module' ,
163
+ async : true ,
164
+ dangerouslySetInnerHTML : script ,
165
+ nonce,
151
166
} )
152
167
) ;
168
+
169
+ const core = resolvedManifest ?. manifest . core ;
153
170
if ( core ) {
154
171
beforeContent . push ( jsx ( 'link' , { rel : 'modulepreload' , href : `${ base } ${ core } ` } ) ) ;
155
172
}
0 commit comments