Add PWA support to Vite
Add installable PWA support to a Vite app with vite-plugin-pwa, which writes the web app manifest and a service worker that precaches built files.
vite-plugin-pwa generates the manifest and a Workbox service worker from your build output.
npm install -D vite-plugin-pwaimport { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
export default defineConfig({
plugins: [
VitePWA({
registerType: 'autoUpdate',
manifest: {
name: 'Example App',
short_name: 'Example',
theme_color: '#0f172a',
icons: [
{ src: 'icon-192.png', sizes: '192x192', type: 'image/png' },
{ src: 'icon-512.png', sizes: '512x512', type: 'image/png' },
],
},
}),
],
});PWA v1.0.0
mode generateSW
precache 8 entries (142.31 KiB)
files generated
dist/sw.js
dist/workbox-5ffe50d4.js
dist/manifest.webmanifestThe plugin lists every built file in the service worker's precache, so the app opens offline after the first visit. autoUpdate swaps in a new worker when you deploy. Browsers install a PWA only over HTTPS or on localhost. See the vite-plugin-pwa guide.
Precaching a large app costs the visitor a big first download. Exclude heavy assets with workbox.globIgnores. Version numbers and sizes in the output are an example.