Skip to content

Commit d78d69d

Browse files
committed
fix(preloader): race condition on load + refactor
1 parent 3f49ff2 commit d78d69d

File tree

5 files changed

+8
-12
lines changed

5 files changed

+8
-12
lines changed

packages/qwik/src/core/preloader/bundle-graph.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isBrowser } from '@builder.io/qwik/build';
2-
import { config, doc } from './constants';
2+
import { config, isJSRegex } from './constants';
33
import { adjustProbabilities, bundles, log, shouldResetFactor, trigger } from './queue';
44
import type { BundleGraph, BundleImport, ImportProbability } from './types';
55
import { BundleImportState_None, BundleImportState_Alias } from './types';
@@ -8,15 +8,9 @@ export let base: string | undefined;
88
export let graph: BundleGraph;
99

1010
const makeBundle = (name: string, deps?: ImportProbability[]) => {
11-
const url = name.endsWith('.js')
12-
? doc
13-
? new URL(`${base}${name}`, doc.baseURI).toString()
14-
: name
15-
: null;
1611
return {
1712
$name$: name,
18-
$url$: url,
19-
$state$: url ? BundleImportState_None : BundleImportState_Alias,
13+
$state$: isJSRegex.test(name) ? BundleImportState_None : BundleImportState_Alias,
2014
$deps$: shouldResetFactor ? deps?.map((d) => ({ ...d, $factor$: 1 })) : deps,
2115
$inverseProbability$: 1,
2216
$createdTs$: Date.now(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export const rel =
1919

2020
// Global state
2121
export const loadStart = Date.now();
22+
23+
export const isJSRegex = /\.[mc]?js$/;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ test('preloader script', () => {
2121
* dereference objects etc, but that actually results in worse compression
2222
*/
2323
const compressed = compress(Buffer.from(preLoader), { mode: 1, quality: 11 });
24-
expect([compressed.length, preLoader.length]).toEqual([1785, 5298]);
24+
expect([compressed.length, preLoader.length]).toEqual([1785, 5285]);
2525
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ const preloadOne = (bundle: BundleImport) => {
121121
);
122122

123123
const link = doc.createElement('link');
124-
link.href = bundle.$url$!;
124+
// Only bundles with state none are js bundles
125+
link.href = new URL(`${base}${bundle.$name$}`, doc.baseURI).toString();
125126
link.rel = rel;
126127
// Needed when rel is 'preload'
127128
link.as = 'script';
@@ -208,7 +209,7 @@ export const handleBundle = (name: string, inverseProbability: number) => {
208209
};
209210

210211
export const preload = (name: string | (number | string)[], probability?: number) => {
211-
if (base == null || !name.length) {
212+
if (!name?.length) {
212213
return;
213214
}
214215

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export type BundleInfo = {
1212

1313
export type BundleImport = BundleInfo & {
1414
$name$: string;
15-
$url$: string | null;
1615
$state$: number;
1716
$createdTs$: number;
1817
$waitedMs$: number;

0 commit comments

Comments
 (0)