SvelteKit PWA Setup
Setting up @vite-pwa/sveltekit
Section titled “Setting up @vite-pwa/sveltekit”The @vite-pwa/sveltekit package wraps Workbox inside a Vite plugin that hooks into the SvelteKit build pipeline. It generates the service worker, injects the precache manifest, and creates the web app manifest — all from a single plugin call.
Installation
Section titled “Installation”npm install -D @vite-pwa/sveltekitAdding the plugin to vite.config.ts
Section titled “Adding the plugin to vite.config.ts”Open (or create) vite.config.ts at the project root and add SvelteKitPWA after sveltekit():
import { sveltekit } from '@sveltejs/kit/vite';import { SvelteKitPWA } from '@vite-pwa/sveltekit';import { defineConfig } from 'vite';
export default defineConfig({ plugins: [ sveltekit(), SvelteKitPWA({ registerType: 'autoUpdate', manifest: { name: 'My SvelteKit App', short_name: 'SK App', description: 'An offline-capable SvelteKit PWA', theme_color: '#ff3e00', background_color: '#ffffff', display: 'standalone', start_url: '/', icons: [ { src: '/pwa-192x192.png', sizes: '192x192', type: 'image/png', }, { src: '/pwa-512x512.png', sizes: '512x512', type: 'image/png', }, ], }, }), ],});Key options
Section titled “Key options”registerType
Section titled “registerType”Controls how the service worker handles updates:
| Value | Behaviour |
|---|---|
'autoUpdate' | New SW activates immediately on install — no user prompt |
'prompt' | New SW waits; your UI code decides when to apply the update |
For most production apps, 'prompt' is safer — it prevents a mid-session asset mismatch while the user still has the old page loaded.
manifest
Section titled “manifest”The plugin injects a manifest.webmanifest into the build output and adds the <link rel="manifest"> tag to every page. The name, icons, and theme_color fields are required for Chrome’s installability criteria.
strategies
Section titled “strategies”The default strategy is 'generateSW' — the plugin generates a complete Workbox-powered service worker for you. Switch to 'injectManifest' when you need to write your own SW logic and only want Workbox to inject the precache list:
SvelteKitPWA({ strategies: 'injectManifest', srcDir: './src', filename: 'my-sw.ts', injectManifest: { injectionPoint: 'self.__WB_MANIFEST', globPatterns: ['client/**/*.{js,css,ico,png,svg,webp}'], },})Your custom SW file must contain the self.__WB_MANIFEST injection point — Workbox replaces it with the generated precache list at build time.
Static manifest file
Section titled “Static manifest file”If you prefer a static file over the generated one, place manifest.webmanifest in the static/ directory and skip the manifest option in the plugin. SvelteKit copies everything in static/ to the build output verbatim.
{ "name": "My SvelteKit App", "short_name": "SK App", "start_url": "/", "display": "standalone", "theme_color": "#ff3e00", "background_color": "#ffffff", "icons": [ { "src": "/pwa-192x192.png", "sizes": "192x192", "type": "image/png" }, { "src": "/pwa-512x512.png", "sizes": "512x512", "type": "image/png" } ]}Trade-offs
Section titled “Trade-offs”| Option | Benefit | Cost |
|---|---|---|
Use @vite-pwa/sveltekit | Sets up manifest + service worker in one call, works with SvelteKit’s adapters | Tied to a specific ecosystem, you keep the plugin updated separately from SvelteKit itself |
Write your own setup directly with the $service-worker module | Full control over every detail without a plugin | You handle integration with the adapter (static/node/vercel) entirely yourself, adding edge cases |
Common mistakes
Section titled “Common mistakes”- Configuring the plugin without checking whether the adapter in use (
adapter-static,adapter-node, etc.) supports the kind of service worker you want. - Forgetting to turn on
devOptions.enabled: trueand being confused about why the service worker does not register in dev. - Not testing against a production build (
vite build && vite preview), missing bugs that only show up in the real build.
@vite-pwa/sveltekit is part of the same Vite PWA ecosystem that Elk, the open-source Mastodon client, ports the same ideas to on SvelteKit, and many SvelteKit community starter templates use this plugin as their standard starting point for a SvelteKit PWA.