OpenChat icon indicating copy to clipboard operation
OpenChat copied to clipboard

On Vercel, the service worker is written outside the deployed output and precaches nothing

Open ajanraj opened this issue 1 month ago • 0 comments

Problem

On Vercel, the PWA build writes sw.js where Vercel doesn't deploy it, and the service worker's precache list is empty.

vite.config.ts hard-codes VitePWA({ outDir: ".output/public" }) (added in #55). That's Nitro's public directory for the default node-server preset. Vercel builds use the vercel preset (Nitro detects Vercel automatically), and that preset writes client assets to .vercel/output/static. So the PWA plugin:

  • searches the empty .output/public for files to precache, and
  • writes sw.js and workbox-*.js into .output/public, which Vercel doesn't deploy. registerSW.js does land in .vercel/output/static, so it would point at a missing /sw.js.

Repro

rm -rf .output .vercel/output
NODE_ENV=production NITRO_PRESET=vercel bun run build

(NODE_ENV=production works around #88.)

PWA v1.3.0
mode      generateSW
precache  3 entries (0.00 KiB)
files generated
  .output/public/sw.js
  .output/public/workbox-e190f46a.js
warnings
  One of the glob patterns doesn't match any files. ... "globDirectory": ".../.output/public"

.vercel/output/static contains registerSW.js but no sw.js. CI builds with the node-server preset, where the client assets really are in .output/public, so the same config precaches 681 entries there.

Checked with a local NITRO_PRESET=vercel build of main (17ade4e), not against the live deployment. #87 (vite-plugin-pwa 2.0) doesn't change this: v2 still derives globDirectory and swDest from outDir.

Expected

On Vercel, the service worker is generated in the directory Vercel serves, its precache list covers the client assets, and /sw.js is deployed.

Possible fixes

  • Point outDir at the preset's public directory instead of hard-coding it, e.g. .vercel/output/static when building on Vercel. Under the vercel preset, the client build already writes there, so the glob would find the assets.
  • After any fix, check that the precache line in the Vercel build log is non-empty and that /sw.js is served.

ajanraj avatar Oct 16 '26 21:10 ajanraj