@vite-pwa/sveltekit
v2.0.4
Published
Zero-config PWA for SvelteKit
Readme
🚀 Features
- 📖 Documentation & guides
- 👌 Zero-Config: sensible built-in default configs for common use cases
- 🔩 Extensible: expose the full ability to customize the behavior of the plugin
- 🦾 Type Strong: written in TypeScript
- 🔌 Offline Support: generate service worker with offline support (via Workbox)
- ⚡ Fully tree shakable: client virtual modules are strictly optimized for Dead Code Elimination.
- 📄 Web Manifest: auto-inject Web App Manifest and PWA assets seamlessly.
- 💬 Prompt for new content: built-in support for Vanilla JavaScript, Vue 3, React, Svelte, SolidJS and Preact
- ⚙️ Stale-while-revalidate: automatic reload when new content is available
- ✨ Static assets handling: configure static assets for offline support
- 🐞 Development Support: debug your custom service worker logic as you develop your application
- 🔍 Inspector: visually inspect your resolved configuration, web manifest and service worker during development, via middleware or DevFrame
- 🧩 Modular: use the ready-to-use plugins or compose your own integration from the building blocks in
@unplugin-pwa/coreand@unplugin-pwa/vite - 📦 Universal Bundlers: native integration with Vite, Rolldown, Webpack (WIP), Rspack (WIP), and Rsbuild (WIP).
- 🛠️ Meta-Frameworks: seamless integration with îles, SvelteKit, VitePress, Astro, Nuxt 3/4/5, React Router, TanStack and Next (WIP).
- 💥 PWA Assets Generator: generate all the PWA assets from a single command and a single source image
- 🚀 PWA Assets Integration: serving, generating and injecting PWA Assets on the fly in your application
📦 Install
From v2.0.0,
@vite-pwa/sveltekitsupports SvelteKit 3 (should also support SvelteKit 1) and requires Node 22.14.0 or above.
From v0.3.0,
@vite-pwa/sveltekitsupports SvelteKit 2 (should also support SvelteKit 1).
From v0.2.0,
@vite-pwa/sveltekitrequires SvelteKit 1.3.1 or above.
npm i @vite-pwa/sveltekit -D
# yarn
yarn add @vite-pwa/sveltekit -D
# pnpm
pnpm add @vite-pwa/sveltekit -D🦄 Usage
⚠️ v2.0.0 Breaking Changes
- SvelteKit 3 Support: The SvelteKit integration has been completely revamped. SvelteKit v3 removes the internal
$service-workermodule, andv2.0.0aligns with this new architecture. - API Change: The standalone
SvelteKitPWA()plugin has been replaced by thewithPwawrapper, which now manages thesveltekit()plugin execution internally to correctly inject the PWA context and adapter. - SvelteKit >= 2.63.0: The SvelteKit team backported the ability to pass the
svelte.config.jsconfiguration directly into the SvelteKit Vite plugin. If you are usingv2.63.0+or Kit v3, you can directly use the newwithPwawrapper. The plugin automatically detects the Kit version to conditionally enable the new Vite Environment API in the PWA context without manual configuration. - SvelteKit < 2.63.0 with
buildSW: If you are still using an older SvelteKit v2 version and opting for thebuildSWstrategy (formerlyinjectManifest), you must manually remove the service worker entry from yoursvelte.config.jsto avoid conflicts with Kit's legacy SW behavior.- Legacy support (SvelteKit < 2.63.0): From
v2.0.0you cannot register the service worker atsvelte.config.jsanymore, use this package or use SvelteKit's built-in service worker registration.
- Legacy support (SvelteKit < 2.63.0): From
v2.x (SvelteKit 3 & >= 2.63.0)
In v2.0.0, wrap your SvelteKit configuration using withPwa. This function automatically includes the sveltekit() plugin and all necessary @unplugin-pwa integrations.
// vite.config.js / vite.config.ts
import { withPwa } from '@vite-pwa/sveltekit'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
withPwa(
// SvelteKit options (formerly passed to sveltekit())
{
// ...
},
// PWA options
{
strategy: 'buildSW',
// ...
}
)
]
})v2.x (SvelteKit < 2.63.0)
Add LegacySvelteKitPWA plugin to vite.config.js / vite.config.ts and configure it.
// vite.config.js / vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite'
import { LegacySvelteKitPWA } from '@vite-pwa/sveltekit/legacy'
export default {
plugins: [
sveltekit(),
LegacySvelteKitPWA()
]
}Read the 📖 v2.x documentation for a complete guide on how to configure and use this plugin.
v1.x (Legacy SvelteKit < 2.63.0)
Add SvelteKitPWA plugin to vite.config.js / vite.config.ts and configure it.
// vite.config.js / vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite'
import { SvelteKitPWA } from '@vite-pwa/sveltekit'
export default {
plugins: [
sveltekit(),
SvelteKitPWA()
]
}Read the 📖 v1.x documentation for a complete guide on how to configure and use this plugin.
👀 Full config
Check out the type declaration src/types.ts and the following links for more details.
📄 License
MIT License © 2022-PRESENT Anthony Fu
