@@ -14,6 +14,7 @@ export const bundles: BundleImports = new Map();
14
14
export let shouldResetFactor : boolean ;
15
15
let queueDirty : boolean ;
16
16
let preloadCount = 0 ;
17
+ let pendingHref : string | undefined ;
17
18
const queue : BundleImport [ ] = [ ] ;
18
19
const MPA_FALLBACK_THRESHOLD = 100 ;
19
20
@@ -217,8 +218,6 @@ export const adjustProbabilities = (
217
218
* something else, in which case we don't want to block reprioritization of this new event
218
219
* bundles for too long. (If browsers supported aborting modulepreloads, we wouldn't have to
219
220
* do this.)
220
- *
221
- * TODO: Set the limit to a number of kb instead of a number of bundles.
222
221
*/
223
222
if ( probability === 1 || ( probability >= 0.99 && depsCount <= MPA_FALLBACK_THRESHOLD + 1 ) ) {
224
223
depsCount ++ ;
@@ -234,7 +233,7 @@ export const adjustProbabilities = (
234
233
dep . $factor$ = factor ;
235
234
}
236
235
237
- dispatchMPAFallback ( ) ;
236
+ fallbackToMpa ( ) ;
238
237
239
238
adjustProbabilities ( depBundle , newInverseProbability , seen ) ;
240
239
}
@@ -296,15 +295,40 @@ if (isBrowser) {
296
295
* ~10s (usually between 3-7s).
297
296
*
298
297
* Note: if the next route bundles have already been preloaded, the fallback won't be triggered.
299
- *
300
- * TODO: get total kb size and compare with 100kb instead of relying on the number of bundles.
301
298
*/
302
- const dispatchMPAFallback = ( ) => {
299
+ const fallbackToMpa = ( ) => {
300
+ if ( ! pendingHref ) {
301
+ return ;
302
+ }
303
303
const nextRouteBundles = queue . filter ( ( item ) => item . $inverseProbability$ <= 0.1 ) ;
304
304
if ( nextRouteBundles . length >= MPA_FALLBACK_THRESHOLD ) {
305
- const href = ( window as any ) . __qwikPendingFallbackHref ;
306
- if ( href ) {
307
- window . location . href = href ;
305
+ if ( pendingHref !== window . location . href ) {
306
+ window . location . href = pendingHref ;
307
+ }
308
+ }
309
+ } ;
310
+
311
+ /**
312
+ * Sets the MPA fallback href. When too many bundles are queued for preloading, the preloader will
313
+ * redirect to this href using the browser navigation.
314
+ *
315
+ * @param href - The target URL for MPA fallback. Should be an absolute URL string or null/undefined
316
+ * to clear it.
317
+ * @returns Void
318
+ */
319
+ export const setMpaFallbackHref = ( href : string | null | undefined ) => {
320
+ if ( ! href || typeof href !== 'string' ) {
321
+ pendingHref = undefined ;
322
+ return ;
323
+ }
324
+
325
+ try {
326
+ const url = new URL ( href , window . location . origin ) ;
327
+ pendingHref = url . href ;
328
+ } catch ( error ) {
329
+ pendingHref = undefined ;
330
+ if ( config . $DEBUG$ ) {
331
+ console . warn ( '[Qwik Preloader] Invalid href provided to setSpaPendingHref:' , href ) ;
308
332
}
309
333
}
310
334
} ;
0 commit comments