Skip to content

Commit 22c6498

Browse files
committed
wip 2: don't use preload for low prio
1 parent d6e5953 commit 22c6498

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

packages/qwik/src/core/preloader.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ let base: string | undefined;
4949
const doc = isBrowser ? document : undefined!;
5050
const modulePreloadStr = 'modulepreload';
5151
const preloadStr = 'preload';
52+
const pushStr = 'push';
53+
const unshiftStr = 'unshift';
5254

5355
const makeBundle = (path: string, imports: string[], dynamicImports: string[]) => {
5456
const url = path.endsWith('.js') ? new URL(`${base}${path}`, doc.baseURI).toString() : null;
@@ -86,11 +88,11 @@ const rel =
8688
let highCount = 0;
8789
let lowCount = 0;
8890
const trigger = () => {
89-
while (highCount < 4 && high.length) {
91+
while (highCount < 6 && high.length) {
9092
const bundle = high.pop()!;
9193
makePreloadLink(bundle!, true);
9294
}
93-
while (lowCount < 2 && low.length) {
95+
while (lowCount < 3 && low.length) {
9496
const bundle = low.pop()!;
9597
makePreloadLink(bundle!);
9698
}
@@ -103,13 +105,11 @@ const makePreloadLink = (bundle: BundleImport, priority?: boolean) => {
103105
bundle.$state$ = BundleImportState.Loading;
104106
const link = doc.createElement('link');
105107
link.href = bundle.$url$!;
108+
link.rel = rel;
106109
if (priority) {
107110
highCount++;
108-
link.rel = rel;
109111
} else {
110112
lowCount++;
111-
link.rel = preloadStr;
112-
link.fetchPriority = 'low';
113113
}
114114
link.as = 'script';
115115
link.onload = link.onerror = () => {
@@ -126,16 +126,7 @@ const makePreloadLink = (bundle: BundleImport, priority?: boolean) => {
126126
return 1;
127127
};
128128

129-
const preloadBundle = (bundle: BundleImport, priority: boolean) => {
130-
if (bundle.$state$ > BundleImportState.Queued) {
131-
return;
132-
}
133-
if (bundle.$url$) {
134-
(priority ? high : low).unshift(bundle);
135-
}
136-
bundle.$state$ = priority ? BundleImportState.Queued : BundleImportState.Low;
137-
};
138-
129+
let method = unshiftStr;
139130
/**
140131
* Preload a bundle or bundles. Requires calling loadBundleGraph first.
141132
*
@@ -145,8 +136,11 @@ const preload = (name: string | string[], priority: boolean, seen?: Set<BundleIm
145136
if (!isBrowser || !base) {
146137
return;
147138
}
139+
// todo reverse order
148140
if (Array.isArray(name)) {
141+
method = pushStr;
149142
name.forEach((n) => preload(n, priority));
143+
method = unshiftStr;
150144
return;
151145
}
152146
let bundle = bundles.get(name);
@@ -159,7 +153,12 @@ const preload = (name: string | string[], priority: boolean, seen?: Set<BundleIm
159153
return;
160154
}
161155
seen?.add(bundle);
162-
preloadBundle(bundle, priority);
156+
if (bundle.$state$ < BundleImportState.Queued) {
157+
if (bundle.$url$) {
158+
(priority ? high : low)[method as 'unshift' | 'push'](bundle);
159+
}
160+
bundle.$state$ = priority ? BundleImportState.Queued : BundleImportState.Low;
161+
}
163162
for (const importName of bundle.$imports$) {
164163
preload(importName, priority, seen || new Set([bundle]));
165164
}

packages/qwik/src/server/prefetch-implementation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ function linkJsImplementation(
166166
})
167167
`.replaceAll(/^\s+|\s*\n/gm, '');
168168

169+
// TODO move this to the top of the page
169170
prefetchNodes.push(
170171
jsx('link', {
171172
rel: 'fetch',

scripts/submodule-preloader.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { join } from 'node:path';
22
import { rollup, type InputOptions, type OutputOptions } from 'rollup';
33
import { fileSize, rollupOnWarn, type BuildConfig } from './util';
44
import { minify } from 'terser';
5-
import ts from 'typescript';
5+
import { transform } from 'esbuild';
66

77
/**
88
* Builds the qwikloader javascript files. These files can be used by other tooling, and are
@@ -24,19 +24,16 @@ export async function submodulePreloader(config: BuildConfig) {
2424
return null;
2525
},
2626
async transform(code, id) {
27-
// use typescript to transpile
28-
const result = ts.transpileModule(code, {
29-
compilerOptions: {
30-
module: ts.ModuleKind.ESNext,
31-
// todo get rid of the enum somehow
32-
preserveConstEnums: false,
33-
},
34-
fileName: id,
27+
const result = await transform(code, {
28+
sourcefile: id,
29+
target: 'es2017',
30+
format: 'esm',
31+
loader: 'ts',
3532
});
3633

3734
// Rename $properties$ to short names but leave the rest legible
3835
// The final app will minify it when needed
39-
const minified = await minify(result.outputText, {
36+
const minified = await minify(result.code, {
4037
compress: false,
4138
mangle: {
4239
toplevel: false,

0 commit comments

Comments
 (0)