OpenChat
OpenChat copied to clipboard
Service worker is built but never registered
Problem
The build generates sw.js and registerSW.js, but no page loads registerSW.js or calls navigator.serviceWorker.register, so no service worker is ever installed. Precaching, registerType: "autoUpdate" and the Google Fonts runtime caching in vite.config.ts all do nothing.
Repro
NODE_ENV=production bun run build
NODE_ENV=production bunx vite preview
GET /sw.jsandGET /registerSW.jsreturn 200.- The HTML for
/includes<link rel="manifest" href="/manifest.json"/>, but nothing mentionsregisterSW,serviceWorkerorsw.js. - Nothing in
src/importsvirtual:pwa-register, and nothing in git history ever did.
Checked on main (17ade4e). #87 (vite-plugin-pwa 2.0) uses the same injection logic.
Cause
VitePWA defaults to injectRegister: "auto". Without a virtual:pwa-register import, that resolves to "script", which adds <script src="/registerSW.js"> in Vite's transformIndexHtml hook. TanStack Start renders HTML on the server and has no index.html, so the hook never runs and the tag is never added.
Expected
Production pages register /sw.js.
Possible fixes
- Register from the app: set
injectRegister: falseand callregisterSW({ immediate: true })fromvirtual:pwa-registerin client-only code, such as an effect in the root component. - Or add
{ src: "/registerSW.js" }to the root route'shead().scriptsinsrc/routes/__root.tsx.
Before fixing:
- Fix #89 first or in the same change.
/sw.jsisn't deployed on Vercel today, so registration there would fail. - Turning the service worker on means every first visit downloads the whole precache in the background: 681 entries (~24 MB) in CI's build of
main.globPatternsmay need trimming.