Skip to content

Service worker is built but never registered #90

Open

Description

@ajanraj

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.js and GET /registerSW.js return 200.
  • The HTML for / includes <link rel="manifest" href="/manifest.json"/>, but nothing mentions registerSW, serviceWorker or sw.js.
  • Nothing in src/ imports virtual: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: false and call registerSW({ immediate: true }) from virtual:pwa-register in client-only code, such as an effect in the root component.
  • Or add { src: "/registerSW.js" } to the root route's head().scripts in src/routes/__root.tsx.

Before fixing:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions