Skip to content

Commit 09a341d

Browse files
committed
wip preload better reprio
1 parent 4cd9f86 commit 09a341d

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

packages/qwik/src/core/preloader.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const enum BundleImportState {
3333
Low,
3434
Queued,
3535
Loading,
36+
Loaded,
3637
}
3738
type BundleImport = {
3839
$name$: string;
@@ -56,6 +57,26 @@ const doc = isBrowser ? document : undefined!;
5657
const modulePreloadStr = 'modulepreload';
5758
const preloadStr = 'preload';
5859

60+
const checkLoaded = (bundle: BundleImport) => {
61+
if (bundle.$state$ === BundleImportState.Loaded) {
62+
return true;
63+
}
64+
bundle.$imports$ = bundle.$imports$.filter(
65+
(dep) => bundles.get(dep)!.$state$ !== BundleImportState.Loading
66+
);
67+
bundle.$dynamicImports$ = bundle.$dynamicImports$.filter(
68+
(dep) => bundles.get(dep)!.$state$ !== BundleImportState.Loading
69+
);
70+
if (
71+
bundle.$state$ === BundleImportState.Loading &&
72+
!bundle.$imports$.length &&
73+
!bundle.$dynamicImports$.length
74+
) {
75+
bundle.$state$ = BundleImportState.Loaded;
76+
return true;
77+
}
78+
};
79+
5980
let highCount = 0;
6081
let lowCount = 0;
6182
/**
@@ -67,11 +88,12 @@ let lowCount = 0;
6788
* We make sure to first empty the high priority items, first-in-last-out.
6889
*/
6990
const trigger = () => {
70-
while (highCount < 8 && high.length) {
91+
// high is confirmed needed so we go as wide as possible
92+
while (highCount < 20 && high.length) {
7193
const bundle = high.pop()!;
7294
preloadOne(bundle!, true);
7395
}
74-
while (highCount + lowCount < 4 && low.length) {
96+
while (highCount + lowCount < 3 && low.length) {
7597
const bundle = low.pop()!;
7698
preloadOne(bundle!);
7799
}
@@ -86,7 +108,7 @@ const rel =
86108
* that slows down interaction
87109
*/
88110
const preloadOne = (bundle: BundleImport, priority?: boolean) => {
89-
if (bundle.$state$ === BundleImportState.Loading) {
111+
if (bundle.$state$ === BundleImportState.Loaded) {
90112
return;
91113
}
92114
if (bundle.$url$) {
@@ -120,14 +142,16 @@ const preloadOne = (bundle: BundleImport, priority?: boolean) => {
120142
}
121143

122144
bundle.$state$ = BundleImportState.Loading;
123-
/** Now that we processed the bundle, its dependencies are needed ASAP */
124-
if (priority) {
125-
// make sure to queue the high priority imports first so they preloaded before the low priority ones
126-
preload(bundle.$imports$, priority);
127-
preload(bundle.$dynamicImports$);
128-
} else {
129-
// only preload direct imports, low priority
130-
preload(bundle.$imports$);
145+
if (!checkLoaded(bundle)) {
146+
/** Now that we processed the bundle, its dependencies are needed ASAP */
147+
if (priority) {
148+
// make sure to queue the high priority imports first so they preloaded before the low priority ones
149+
preload(bundle.$imports$, priority);
150+
preload(bundle.$dynamicImports$);
151+
} else {
152+
// only preload direct imports, low priority
153+
preload(bundle.$imports$);
154+
}
131155
}
132156
};
133157

@@ -148,7 +172,7 @@ const ensureBundle = (name: string) => {
148172
bundle = makeBundle(name, [], []);
149173
bundles.set(name, bundle);
150174
}
151-
if (bundle.$state$ === BundleImportState.Loading) {
175+
if (checkLoaded(bundle)) {
152176
return;
153177
}
154178
return bundle;

0 commit comments

Comments
 (0)