What this thread does
Add vite-plugin-pwa with a generated worker and update prompt; use a custom worker for specialized fetch behavior.
What changed
- vite-plugin-pwa 2.0.0 release, 2026-10-03 (checked 2026-10-28), expands the assets-generator peer range. Its tagged README raises the Node minimum to 20.19.0 (checked 2026-10-28); do not upgrade the plugin without checking CI and local Node.
- The tagged package metadata declares Vite majors 3 through 8 and Workbox
^7.4.1ranges (checked 2026-10-28). Guide banner 1.2.0 (checked 2026-10-28) lags tagged metadata. - WebKit's Safari 26.0 notes describe iOS/iPadOS 26 opening Home Screen additions as web apps by default (versions checked 2026-10-28). Installation alone still does not implement offline behavior.
How to migrate or add it
1. Check node --version, Vite and existing registrations. Keep framework plugins in vite.config. Follow setup with npm install -D vite-plugin-pwa@2.0.0 (checked 2026-10-28); use one registration per scope. 2. Add the plugin. Create the declared icons in public/; adjust URLs/scope for subpath deployment.
// vite.config.js
import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
export default defineConfig({
plugins: [VitePWA({
strategies: 'generateSW',
registerType: 'prompt',
manifest: {
name: 'My App', short_name: 'My App',
start_url: '/', scope: '/', display: 'standalone',
theme_color: '#ffffff', background_color: '#ffffff',
icons: [
{ src: '/pwa-192x192.png', sizes: '192x192', type: 'image/png' },
{ src: '/pwa-512x512.png', sizes: '512x512', type: 'image/png' }
]
}
})]
});
3. Register once in browser entry code, using the official prompt flow. Add <button id="pwa-update" hidden>Update app</button> first. Also offer Later and preserve drafts in the real UI.
import { registerSW } from 'virtual:pwa-register';
const button = document.querySelector('#pwa-update');
const updateSW = registerSW({
onNeedRefresh() { if (button) button.hidden = false; },
onOfflineReady() { console.info('Offline assets are ready'); },
onRegisterError(error) { console.error(error); }
});
button?.addEventListener('click', () => updateSW(true));
For SSR, register only in browser code with a guarded dynamic import, as the prompt guide shows.
4. Precached app assets do not make API data available offline. Workbox strategies distinguish NetworkFirst for freshness with fallback, CacheFirst for reusable static resources, StaleWhileRevalidate for stale-acceptable content, and NetworkOnly for always-online requests. Cache narrow public GET routes with bounded expiration. Define logout behavior; exclude private API data. Offline mutations need persistence/retry design. 5. If you need custom worker logic, switch to injectManifest, install npm install -D workbox-precaching, and use:
VitePWA({
strategies: 'injectManifest', srcDir: 'src', filename: 'sw.js',
registerType: 'prompt'
})
Then create src/sw.js:
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching';
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST);
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting();
});
The build replaces self.__WB_MANIFEST; top-level self.skipWaiting() bypasses waiting. 6. Fully manual alternative: put sw.js in public/, call navigator.serviceWorker.register('/sw.js') in supported browsers, and implement install/activate/fetch and cache cleanup per the browser guide. Maintain hashed assets and revisioning yourself; do not combine with plugin registration.
Versions, licences and choice — checked 2026-10-28
| Approach | Status | Pick when | Not for |
|---|---|---|---|
| vite-plugin-pwa 2.0.0 + generateSW | MIT, tagged README | App shell and routine caching | Owning every worker event |
| Same plugin + injectManifest | Same licence; Workbox build dependencies in tagged metadata | Custom worker routing and offline behavior | Assuming build-time injection also writes runtime routes |
| Manual browser worker | Browser API; no plugin package | Small, fully controlled worker | Automatic precache revisioning of Vite's hashed output |
Hosting/prices unverified.
What breaks
Diagnostic: Cannot find module
The FAQ gives ts2307 for virtual imports: add /// <reference types="vite-plugin-pwa/client" /> in a declaration file. Oversized precache assets can also fail builds; split bundles before raising maximumFileSizeToCacheInBytes.
Old tabs can keep a worker waiting. A custom worker needs the message handler for the update button. autoUpdate enables immediate activation/claiming and reload behavior; this can discard unsaved state. skipWaiting controls activation, while clientsClaim controls taking clients; neither migrates in-memory application state for you.
What to test
With the scripts from Vite's deployment guide, run npm run build then npm run preview (PWA testing guide). In DevTools Application, inspect manifest, registration scope and Cache Storage. Load online first, then go offline and reload the start URL and a deep link; try a cached screen and a never-fetched API request.
With an edited form open, serve another build at the same origin. Trigger update checks and test Later/Update, multiple tabs, expired login and rollback. Disable DevTools Update on reload for this test. Use HTTPS outside localhost.
Lighthouse performance/accessibility audits are useful, but Chrome marks Lighthouse PWA testing deprecated; a score does not prove offline correctness. Test on a real iPhone too. WebKit's push guide requires Home Screen installation and direct user interaction for its permission flow. Do not assume Chromium install prompts, durable caches or background behavior are identical on iOS.
Community evidence and pitfalls
- danielalejandromatheus reports “not reloading the page” in issue #896. Maintainer userquin points to reactive UI integration and timing between worker builds. Forced reload is not a verified fix.
- IlariaCallegari describes a missing prompt and old assets returning in discussion #821. The fetched page was unanswered; no cause is established.
A prompt to give your agent
Your training data predates this release. Read the linked tagged README, release, prompt and injectManifest guides; do not guess requirements or flags. Inspect our Vite base URL, worker and unsaved-state flows. Add one registration and update prompt; explain cache routes. Test offline reload and two releases with multiple tabs. Report untested iOS behavior; exclude private API data.
Limits
No build, lifecycle or device tests. Two reports do not measure success.
Started from a post by @danielalejandromatheus, packaged by the editor. Original post