qwik icon indicating copy to clipboard operation
qwik copied to clipboard

`service-worker.js` has `basePathname` prepended twice in `@qwik-city-sw-register`

Open jessezhang91 opened this issue 3 years ago • 1 comments

Qwik Version

0.11.1

Operating System (or Browser)

OSX

Node Version (if applicable)

16.14.0

Which component is affected?

Qwik City

Expected Behaviour

When using basePathname with the QwikCity Vite plugin, @qwik-city-sw-register will produce a service-worker.js url with basePathname prepended twice. I expect it to prepend only once.

Expected: @qwik-city-sw-register to include /<basePathname>/service-worker.js.

Actual Behaviour

@qwik-city-sw-register includes /<basePathname>/<basePathname>/service-worker.js.

Worth noting that despite the basePathname being prepended twice, the service-worker.js does load properly because it's also emitted as dist/<basePathname>/service-worker.js.

Additional Information

vite.config.ts

import { defineConfig } from 'vite';
import { qwikVite } from '@builder.io/qwik/optimizer';
import { qwikCity } from '@builder.io/qwik-city/vite';
import tsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig(() => {
  return {
    plugins: [
      qwikCity({
        basePathname: '/base/',
      }),
      qwikVite(),
      tsconfigPaths(),
    ],
  };
});

Print of @qwik-city-sw-register

((s,a,i,r)=>{i=(e,t)=>{t=document.querySelector("[q\\:base]"),t&&a.active&&a.active.postMessage({type:"qprefetch",base:t.getAttribute("q:base"),...e})},document.addEventListener("qprefetch",e=>{const t=e.detail;a?i(t):t.bundles&&s.push(...t.bundles)}),navigator.serviceWorker.register("/base/base/service-worker.js").then(e=>{r=()=>{a=e,i({bundles:s})},e.installing?e.installing.addEventListener("statechange",t=>{t.target.state=="activated"&&r()}):e.active&&r()}).catch(e=>console.error(e))})([])

Note the /base/base/service-worker.js in the above print.

Digging into the code a little bit, I see the basePathname is added once as part of generating the service worker register code here: https://github.com/BuilderIO/qwik/blob/bcfd868117fd02d7ddb1dcf02c82ad11b84de444/packages/qwik-city/buildtime/runtime-generation/generate-service-worker.ts#L20

and once again here: https://github.com/BuilderIO/qwik/blob/bcfd868117fd02d7ddb1dcf02c82ad11b84de444/packages/qwik-city/buildtime/routing/resolve-source-file.ts#L195-L197 where the chunkFileName will include the basePathname via getPathnameFromDirPath here: https://github.com/BuilderIO/qwik/blob/bcfd868117fd02d7ddb1dcf02c82ad11b84de444/packages/qwik-city/utils/fs.ts#L32

I think the correct fix here might be to have the service-worker chunkFileName to not include the basePathname since that also affects how it's emitted into dist.

jessezhang91 avatar Oct 23 '22 15:10 jessezhang91

I can submit a PR for this if the fix I had above seems reasonable

jessezhang91 avatar Oct 26 '22 18:10 jessezhang91