From f6b008813c56b761c476e92411ae9e4a1513d43e Mon Sep 17 00:00:00 2001 From: gioboa Date: Thu, 20 Jun 2024 09:04:53 +0200 Subject: [PATCH] fix: fix up SW prefetch --- packages/qwik/src/core/components/prefetch.ts | 12 ++++-------- .../src/prefetch-service-worker/process-message.ts | 5 ++++- packages/qwik/src/server/prefetch-utils.ts | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/qwik/src/core/components/prefetch.ts b/packages/qwik/src/core/components/prefetch.ts index f0b220977d6..574bba4d865 100644 --- a/packages/qwik/src/core/components/prefetch.ts +++ b/packages/qwik/src/core/components/prefetch.ts @@ -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'; /** @@ -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) { @@ -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, diff --git a/packages/qwik/src/prefetch-service-worker/process-message.ts b/packages/qwik/src/prefetch-service-worker/process-message.ts index 6c84144c34e..bbb7ed85aa5 100644 --- a/packages/qwik/src/prefetch-service-worker/process-message.ts +++ b/packages/qwik/src/prefetch-service-worker/process-message.ts @@ -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 { diff --git a/packages/qwik/src/server/prefetch-utils.ts b/packages/qwik/src/server/prefetch-utils.ts index 27ecc7a1a49..7fbdb573429 100644 --- a/packages/qwik/src/server/prefetch-utils.ts +++ b/packages/qwik/src/server/prefetch-utils.ts @@ -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[]) {