vite-pwa plugin does not work in production builds
dev-sw.js works, but the core issue is that sw.js doesn't get built in prod output.
is that possible that rollup might be causing the issue? if so, how to ignore that during the build.
here's my repo with a workaround:
https://github.com/kulterryan/tanstack-start-pwa
GitHub
GitHub - kulterryan/tanstack-start-pwa
Contribute to kulterryan/tanstack-start-pwa development by creating an account on GitHub.
24 Replies
fair-rose•4mo ago
from my (and gpt5's) investigation i believe vite-plugin-pwa doesn't work[1] due to the way it detects the server/client vite environment, doesn't seem to be properly fixed for multi-environment
and even when I manually edited the package, couldn't figure out a proper way to get the worker to build somewhere other than dist/sw.js[2]
[1]: "doesn't work" meaning that the plugin is seemingly not executed when running
vite build with VitePWA() and tanstackStart() both present. as soon as you comment out the tanstackStart() plugin, the service worker gets generated to dist/sw.js and the assets generator runs if configured. which convinced me it was some environment detection/compatability issue
[2]: this might just be due to my inexperience with configuring vite, and I didn't want to use a copy script. either way the ideal behaviour is that the sw gets built into tanstack start's client-dist, so it can also get picked up by nitro publicAssets[]fair-rose•4mo ago
https://github.com/vite-pwa/vite-plugin-pwa/blob/4385f0a7f03b628ca0d635c07042a1031631ffb4/src/plugins/build.ts#L44
this.environment.config.consumer might be required rather than just checking for PWAPluginContext.viteConfig.build.ssr (not too sure)GitHub
vite-plugin-pwa/src/plugins/build.ts at 4385f0a7f03b628ca0d635c0704...
Zero-config PWA for Vite. Contribute to vite-pwa/vite-plugin-pwa development by creating an account on GitHub.
fair-rose•4mo ago
my temporary solution was a bit different
i ended up making a custom script using esbuild and workbox injectManifest, which runs through a vite plugin after client build, but before server (so it gets copied to nitro static assets)
vite plugin:
and then just loaded it with
workbox-window in __root.tsx
but generally PWA support and also the pwa plugin need some work it seemsstormy-gold•4mo ago
is the pwa vite plugin supporting vite's env api?
fair-rose•4mo ago
i don't think so
stormy-gold•4mo ago
start is fully based on the env api. so would need that pwa plugin to support that most likely
fair-rose•4mo ago
oh looks like there is a PR
fair-rose•4mo ago
GitHub
feat!: add Vite 6 Environment API support by userquin · Pull Reque...
Description
2024-11-15: this PR only tested with VanillaJS/TypeScript via test:vite-ecosystem-ci script, rn cannot be tested with lib frameworks, Vite ecosystem-ci is failing for vue and svelte (p...
stormy-gold•4mo ago
nice
fair-rose•4mo ago
hm

stormy-gold•4mo ago
can you test the pkr-new preview build?
fair-rose•4mo ago
sure, gimme a sec
stormy-gold•4mo ago
this might still need some work but it would be good to know the state of it
fair-rose•4mo ago
looks like a similiar situation
dev-sw.js works in dev server
but no pwa/sw steps are ran in build
fair-rose•4mo ago

fair-rose•4mo ago
working in dev though

stormy-gold•4mo ago
can you please check whether we have an open issue about this already on github?
if yes, please add your findings there, otherwise create a new one.
fair-rose•4mo ago
i believe there isn't
stormy-gold•4mo ago
i expect the prod issue to be resolvable with vite 7 as it adds a new buildApp hook for the env api which the plugin should register to build its own env. we should be able to upstream this to the pwa plugin then
fair-rose•4mo ago
just to doublecheck, it is the router repo right
(possibly silly question)
stormy-gold•4mo ago
yes router repo
fair-rose•4mo ago
GitHub
vite-plugin-pwa incompatible with tanstack start production builds ...
Which project does this relate to? Start Describe the bug vite-plugin-pwa's build steps (generate assets, generate serviceworker bundle) are seemingly not executed when running vite build with ...
national-goldOP•4mo ago
Found this for PWA, might be helpful.
https://serwist.pages.dev/docs/vite/getting-started
Getting started - @serwist/vite - Serwist
A Swiss Army knife for service workers.
passive-yellow•4mo ago
I modified the Astro intergration vite-pwa has and it works like a charm, even though it's not clean