Skip to content

Commit

Permalink
fix: fix up SW prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gioboa committed Jun 20, 2024
1 parent d822538 commit f6b0088
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
12 changes: 4 additions & 8 deletions packages/qwik/src/core/components/prefetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDev } from '@builder.io/qwik/build';
import { _jsxC } from '../internal';
import type { JSXNode } from '@builder.io/qwik/jsx-runtime';
import { _jsxC } from '../internal';
import { useServerData } from '../use/use-env-data';

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export const PrefetchServiceWorker = (opts: {
} else {
// base: '/'
// path: 'qwik-prefetch-service-worker.js
resolvedOpts.path = resolvedOpts.base + resolvedOpts.path;
resolvedOpts.path = resolvedOpts.base.replace('build/', '') + resolvedOpts.path;
}
// dev only errors
if (isDev) {
Expand Down Expand Up @@ -147,14 +147,10 @@ export const PrefetchGraph = (
path: 'qwik-prefetch-service-worker.js',
...opts,
};
const args = [
'graph-url',
resolvedOpts.base,
resolvedOpts.base + `q-bundle-graph-${resolvedOpts.manifestHash}.json`,
]
const args = ['graph-url', resolvedOpts.base, `q-bundle-graph-${resolvedOpts.manifestHash}.json`]
.map((x) => JSON.stringify(x))
.join(',');
const code = `(window.qwikPrefetchSW||(window.qwikPrefetchSW=[])).push(${args})`;
const code = `(window.qwikPrefetchSW||(window.qwikPrefetchSW=[])).push([${args}])`;
const props = {
dangerouslySetInnerHTML: code,
nonce: opts.nonce,
Expand Down
5 changes: 4 additions & 1 deletion packages/qwik/src/prefetch-service-worker/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ async function processBundleGraphUrl(swState: SWState, base: string, graphPath:
}

function processPrefetch(swState: SWState, basePath: string, bundles: string[]) {
const base = swState.$bases$.find((base) => basePath === base.$path$);
let base = swState.$bases$.find((base) => base.$graph$.includes(bundles[0].replace('./', '')));
if (!base) {
base = swState.$bases$.find((base) => basePath === base.$path$);
}
if (!base) {
console.error(`Base path not found: ${basePath}, ignoring prefetch.`);
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/server/prefetch-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function prefetchUrlsEventScript(base: string, prefetchResources: Prefetc
const data: QPrefetchData = {
bundles: flattenPrefetchResources(prefetchResources).map((u) => u.split('/').pop()!),
};
const args = ['prefetch', base, data.bundles!].map((x) => JSON.stringify(x)).join(',');
return `window.qwikPrefetchSW||(window.qwikPrefetchSW=[]).push(${args});`;
const args = ['prefetch', base, ...data.bundles!].map((x) => JSON.stringify(x)).join(',');
return `(window.qwikPrefetchSW||(window.qwikPrefetchSW=[])).push([${args}]);`;
}

export function flattenPrefetchResources(prefetchResources: PrefetchResource[]) {
Expand Down

0 comments on commit f6b0088

Please sign in to comment.