Skip to content

Commit adf20ca

Browse files
authored
Merge pull request #7607 from QwikDev/tweak-preloader
- don't decrease the probability of array preloads - limit ssr preloads to the first 15 qrls on the page - allow somewhat more ssr preloads - when preloading at 100%, also preload the entire dynamic graph at 95%
2 parents f90e6a1 + 7527b80 commit adf20ca

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ 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).toBe(1716);
25-
expect(preLoader.length).toBe(5114);
24+
expect(compressed.length).toBe(1722);
25+
expect(preLoader.length).toBe(5107);
2626
});

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,16 @@ export const adjustProbabilities = (
179179
const prevAdjust = dep.$factor$;
180180
/**
181181
* The chance that a dep won't be loaded is 1-(the chance that the dep will be loaded)*(the
182-
* chance that the current bundle will be loaded)
182+
* chance that the current bundle will be loaded).
183183
*
184184
* We can multiply this chance together with all other bundle adjustments to get the chance
185-
* that a dep will be loaded given all the chances of the other bundles
185+
* that a dep will be loaded given all the chances of the other bundles.
186+
*
187+
* But when we're very likely to load the current bundle, make the dynamic imports more likely
188+
* too.
186189
*/
187-
const newInverseProbability = 1 - dep.$probability$ * probability;
190+
const newInverseProbability =
191+
dep.$probability$ !== 1 && adjustFactor < 0.1 ? 0.05 : 1 - dep.$probability$ * probability;
188192

189193
/** We need to undo the previous adjustment */
190194
const factor = newInverseProbability / prevAdjust;
@@ -216,7 +220,6 @@ export const preload = (name: string | (number | string)[], probability?: number
216220
inverseProbability = 1 - item / 10;
217221
} else {
218222
handleBundle(item, inverseProbability);
219-
inverseProbability *= 1.005;
220223
}
221224
}
222225
} else {

packages/qwik/src/server/preload-impl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Fragment, jsx, type JSXNode } from '@builder.io/qwik';
22
import type { ResolvedManifest } from '../optimizer/src/types';
3-
import { expandBundles } from './prefetch-strategy';
3+
import { expandBundles } from './preload-strategy';
44
import type { PreloaderOptions } from './types';
55

66
export function includePreloader(
@@ -126,8 +126,8 @@ function normalizePreLoaderOptions(
126126
}
127127

128128
const PreLoaderOptionsDefault: Required<PreloaderOptions> = {
129-
ssrPreloads: 5,
130-
ssrPreloadProbability: 0.7,
129+
ssrPreloads: 7,
130+
ssrPreloadProbability: 0.5,
131131
debug: false,
132132
maxIdlePreloads: 25,
133133
preloadProbability: 0.35,

packages/qwik/src/server/prefetch-strategy.ts renamed to packages/qwik/src/server/preload-strategy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ResolvedManifest } from '@builder.io/qwik/optimizer';
22
import { getQueue, preload, resetQueue } from '../core/preloader/queue';
33
import type { QRLInternal } from '../core/qrl/qrl-class';
4-
import { flattenPrefetchResources } from './prefetch-utils';
4+
import { flattenPrefetchResources } from './preload-utils';
55
import type { RenderToStringOptions, SnapshotResult } from './types';
66
import { getPlatform } from '@builder.io/qwik';
77
import { getSymbolHash } from './platform';
@@ -66,10 +66,12 @@ export const expandBundles = (names: string[], resolvedManifest?: ResolvedManife
6666
resetQueue();
6767

6868
let probability = 0.99;
69-
for (const name of names) {
69+
// we assume that after 15 symbols, we're beyond the first screenful of content
70+
// the preloader will load the rest
71+
for (const name of names.slice(0, 15)) {
7072
preload(name, probability);
7173
// later symbols have less probability
72-
probability *= 0.95;
74+
probability *= 0.85;
7375
}
7476

7577
return getQueue().filter(

packages/qwik/src/server/prefetch-utils.unit.ts renamed to packages/qwik/src/server/preload-utils.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert, test } from 'vitest';
2-
import { flattenPrefetchResources } from './prefetch-utils';
2+
import { flattenPrefetchResources } from './preload-utils';
33

44
test('flattenPrefetchResources, no imports', () => {
55
const p = [

packages/qwik/src/server/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { QInstance } from '../core/util/markers';
66
import type { ResolvedManifest, SymbolMapper } from '../optimizer/src/types';
77
import { getSymbolHash, setServerPlatform } from './platform';
88
import { includePreloader } from './preload-impl';
9-
import { getPreloadPaths } from './prefetch-strategy';
9+
import { getPreloadPaths } from './preload-strategy';
1010
import { getQwikLoaderScript } from './scripts';
1111
import type {
1212
QwikManifest,

0 commit comments

Comments
 (0)