Skip to content

Commit d88a145

Browse files
committed
refactor: dispatch userEventPreloads
1 parent 7bfdeb1 commit d88a145

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

packages/qwik-city/src/runtime/src/link-component.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
untrack,
1010
type EventHandler,
1111
type QwikVisibleEvent,
12-
useContext,
1312
} from '@builder.io/qwik';
1413
import { getClientNavPath, shouldPreload } from './utils';
1514
import { loadClientData } from './use-endpoint';
@@ -18,7 +17,7 @@ import { preloadRouteBundles } from './client-navigate';
1817
import { isDev } from '@builder.io/qwik';
1918
// @ts-expect-error we don't have types for the preloader yet
2019
import { p as preload } from '@builder.io/qwik/preloader';
21-
import { fallbackToMpaContext } from './contexts';
20+
// import { fallbackToMpaContext } from './contexts';
2221

2322
/** @public */
2423
export const Link = component$<LinkProps>((props) => {
@@ -37,11 +36,11 @@ export const Link = component$<LinkProps>((props) => {
3736
...linkProps
3837
} = (() => props)();
3938

40-
const defaultFallbackToMpa = useContext(fallbackToMpaContext).default;
39+
// const defaultFallbackToMpa = useContext(fallbackToMpaContext).default;
4140

42-
const fallbackToMpa = __EXPERIMENTAL__.enableFallbackToMpa
43-
? untrack(() => Boolean(fallbackToMpaProp ?? defaultFallbackToMpa))
44-
: undefined;
41+
// const fallbackToMpa = __EXPERIMENTAL__.enableFallbackToMpa
42+
// ? untrack(() => Boolean(fallbackToMpaProp ?? defaultFallbackToMpa))
43+
// : undefined;
4544

4645
const clientNavPath = untrack(() => getClientNavPath({ ...linkProps, reload }, loc));
4746
linkProps.href = clientNavPath || originalHref;
@@ -104,11 +103,23 @@ export const Link = component$<LinkProps>((props) => {
104103
if (!target?.href) {
105104
return;
106105
}
107-
const onTooMany = () => location.assign(target.href);
108-
window.addEventListener('overlySlowReprioritizedPreloading', onTooMany);
106+
const onTooMany = (event: Event) => {
107+
const userEventPreloads = (event as CustomEvent).detail;
108+
/**
109+
* On chrome 3G throttling, 10kb takes ~1s to download. Bundles weight ~1kb on average, so 100
110+
* bundles is ~100kb which takes ~10s to download.
111+
*
112+
* This can serve to fallback to MPA when SPA navigation takes more than 10s. Or in extreme
113+
* cases, if a component needs more than a 100 bundles, display a spinner.
114+
*/
115+
if (userEventPreloads.count >= 100) {
116+
location.assign(target.href);
117+
}
118+
};
119+
window.addEventListener('userEventPreloads', onTooMany);
109120
const url = new URL(target.href);
110121
preloadRouteBundles(url.pathname, 1);
111-
window.removeEventListener('overlySlowReprioritizedPreloading', onTooMany);
122+
window.removeEventListener('userEventPreloads', onTooMany);
112123
});
113124

114125
useVisibleTask$(({ track }) => {

packages/qwik/src/core/preloader/queue.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ let queueDirty: boolean;
1616
let preloadCount = 0;
1717
const queue: BundleImport[] = [];
1818

19-
/**
20-
* On chrome 3G throttling, 10kb takes ~1s to download Bundles weight ~1kb on average, so 100
21-
* bundles is ~100kb which takes ~10s to download.
22-
*
23-
* This can serve to fallback to MPA when SPA navigation takes more than 10s. Or in extreme cases,
24-
* if a component needs more than a 100 bundles, display a spinner.
25-
*/
26-
const OVERLY_SLOW_REPRIORITIZED_PRELOADING_DEFAULT_THRESHOLD = 100;
27-
2819
export const log = (...args: any[]) => {
2920
// eslint-disable-next-line no-console
3021
console.log(
@@ -84,9 +75,9 @@ export const trigger = () => {
8475
sortQueue();
8576
while (queue.length) {
8677
const userEventPreloads = queue.filter((item) => item.$inverseProbability$ <= 0.1);
87-
if (userEventPreloads.length >= OVERLY_SLOW_REPRIORITIZED_PRELOADING_DEFAULT_THRESHOLD) {
88-
dispatchEvent(new CustomEvent('overlySlowReprioritizedPreloading'));
89-
}
78+
dispatchEvent(
79+
new CustomEvent('userEventPreloads', { detail: { count: userEventPreloads.length } })
80+
);
9081

9182
const bundle = queue[0];
9283
const inverseProbability = bundle.$inverseProbability$;
@@ -231,11 +222,7 @@ export const adjustProbabilities = (
231222
* reprioritization of this new event bundles for too long. (If browsers supported aborting
232223
* modulepreloads, we wouldn't have to do this.)
233224
*/
234-
if (
235-
probability === 1 ||
236-
(probability >= 0.99 &&
237-
depsCount <= OVERLY_SLOW_REPRIORITIZED_PRELOADING_DEFAULT_THRESHOLD + 1)
238-
) {
225+
if (probability === 1 || (probability >= 0.99 && depsCount <= 101)) {
239226
depsCount++;
240227
// we're loaded at max probability, so elevate dynamic imports to 99% sure
241228
newInverseProbability = Math.min(0.01, 1 - dep.$importProbability$);

0 commit comments

Comments
 (0)