Skip to content

Commit 884802f

Browse files
committed
feat(core): expose preload and remove qprefetch
1 parent 361e497 commit 884802f

File tree

7 files changed

+16
-30
lines changed

7 files changed

+16
-30
lines changed

packages/qwik-city/src/runtime/src/client-navigate.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { isBrowser } from '@builder.io/qwik';
1+
import { isBrowser, _preload } from '@builder.io/qwik';
22
import type { NavigationType, ScrollState } from './types';
33
import { isSamePath, toPath } from './utils';
4-
import { PREFETCHED_NAVIGATE_PATHS } from './constants';
54

65
export const clientNavigate = (
76
win: Window,
@@ -43,9 +42,7 @@ export const newScrollState = (): ScrollState => {
4342
export const prefetchSymbols = (path: string) => {
4443
if (isBrowser) {
4544
path = path.endsWith('/') ? path : path + '/';
46-
if (!PREFETCHED_NAVIGATE_PATHS.has(path)) {
47-
PREFETCHED_NAVIGATE_PATHS.add(path);
48-
document.dispatchEvent(new CustomEvent('qprefetch', { detail: { links: [path] } }));
49-
}
45+
path = path.length > 1 && path.startsWith('/') ? path.slice(1) : path;
46+
_preload(path, true);
5047
}
5148
};

packages/qwik-city/src/runtime/src/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export const MODULE_CACHE = /*#__PURE__*/ new WeakMap<any, any>();
44

55
export const CLIENT_DATA_CACHE = new Map<string, Promise<ClientPageData | undefined>>();
66

7-
export const PREFETCHED_NAVIGATE_PATHS = new Set<string>();
8-
97
export const QACTION_KEY = 'qaction';
108

119
export const QFN_KEY = 'qfunc';

packages/qwik/src/core/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ export const PrefetchServiceWorker: (opts: {
624624
nonce?: string;
625625
}) => JSXNode_2<'script'>;
626626

627+
// @internal (undocumented)
628+
export const _preload: (name: string, priority: boolean) => void;
629+
627630
// @public (undocumented)
628631
export interface ProgressHTMLAttributes<T extends Element> extends Attrs<'progress', T> {
629632
}

packages/qwik/src/core/internal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export {
1616
} from './use/use-core';
1717
export { _jsxQ, _jsxC, _jsxS } from './render/jsx/jsx-runtime';
1818
export { _fnSignal } from './qrl/inlined-fn';
19+
export { preload as _preload } from './qrl/preload';

packages/qwik/src/core/qrl/preload.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ type BundleImport = {
3232
$dynamicImports$: string[];
3333
};
3434
let bundles: Map<string, BundleImport> | undefined;
35-
type WantedBundle = {
36-
name: string;
37-
priority: boolean;
38-
};
39-
const wantedBundles: Set<WantedBundle> = new Set();
35+
36+
const wantedBundles = new Map<string, boolean>();
4037

4138
const parseBundleGraph = (text: string, base: string) => {
4239
try {
@@ -65,7 +62,7 @@ const parseBundleGraph = (text: string, base: string) => {
6562
$dynamicImports$: dynamicImports,
6663
});
6764
}
68-
for (const { name, priority } of wantedBundles) {
65+
for (const [name, priority] of wantedBundles) {
6966
preload(name, priority);
7067
}
7168
wantedBundles.clear();
@@ -75,6 +72,7 @@ const parseBundleGraph = (text: string, base: string) => {
7572
}
7673
};
7774

75+
/** @internal */
7876
export const loadBundleGraph = (element: Element) => {
7977
if (typeof window === 'undefined' || bundlesP) {
8078
return;
@@ -137,9 +135,10 @@ const preloadBundle = (bundle: BundleImport, priority: boolean) => {
137135
bundle.$state$ = priority ? BundleImportState.Loading : BundleImportState.Low;
138136
};
139137

138+
/** @internal */
140139
export const preload = (name: string, priority: boolean) => {
141140
if (!bundles) {
142-
wantedBundles.add({ name, priority });
141+
wantedBundles.set(name, priority || !!wantedBundles.get(name));
143142
return;
144143
}
145144
const bundle = bundles.get(name);

packages/qwik/src/core/qrl/qrl.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { EMPTY_ARRAY } from '../util/flyweight';
22
import type { QRL } from './qrl.public';
3-
import {
4-
assertQrl,
5-
createQRL,
6-
emitEvent,
7-
getSymbolHash,
8-
isSyncQrl,
9-
type QRLInternal,
10-
} from './qrl-class';
3+
import { assertQrl, createQRL, isSyncQrl, type QRLInternal } from './qrl-class';
114
import { isFunction, isString } from '../util/types';
125
import {
136
qError,
@@ -96,10 +89,6 @@ export const qrl = <T = any>(
9689
if (!announcedQRL.has(symbol)) {
9790
// Emit event
9891
announcedQRL.add(symbol);
99-
emitEvent('qprefetch', {
100-
symbols: [getSymbolHash(symbol)],
101-
bundles: chunk && [chunk],
102-
});
10392
}
10493

10594
// Unwrap subscribers

packages/qwik/src/optimizer/src/plugins/bundle-graph.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getSymbolHash } from 'packages/qwik/src/core/qrl/qrl-class';
12
import type { QwikBundleGraph, QwikManifest } from '../types';
23

34
/**
@@ -84,9 +85,7 @@ export function convertManifestToBundleGraph(
8485
if (!bundle) {
8586
console.warn(`Chunk ${chunkname} for symbol ${symbol} not found in the bundle graph.`);
8687
} else {
87-
const idx = symbol.lastIndexOf('_');
88-
const hash = idx === -1 ? symbol : symbol.slice(idx + 1);
89-
bundleGraph.push(hash, bundle.index);
88+
bundleGraph.push(getSymbolHash(symbol), bundle.index);
9089
}
9190
}
9291
// Second pass to to update dependency pointers

0 commit comments

Comments
 (0)