Getting Started
Getting started with Swoff.
Prerequisites
- Node.js 18+
- A built project with static output (
dist/,public/, etc.) - No existing service worker (or one you're ready to replace)
Install and init
npx @swoff/cli initInteractive wizard: framework (auto-detected), output directory, navigation mode, caching strategy, features. Writes swoff.config.json. Use --yes to skip prompts.
Generate files
npx @swoff/cli generateCreates swoff/ with SW template, injector, generator, client-injector (ESM + IIFE), storage/reset utilities, TS declarations. Feature-specific files (auth, adapters) only for enabled features.
Add build step
The generator must run after each build to embed the latest assets. Add to package.json:
"build": "vite build && node swoff/sw/generator.mjs"Or run manually: node swoff/sw/generator.mjs
Include in your HTML
| Project type | Code |
|---|---|
| Bundler | import { initServiceWorker } from "./swoff/client-injector"; initServiceWorker(); |
| No-bundler | <script src="/swoff/client-injector.bundle.js"></script> |
Verify
Open Chrome DevTools → Application > Service Workers. Check Cache Storage for swoff-precache, swoff-runtime, swoff-runtime-html.
What's next?
- Config Reference — all config fields explained
- Caching Strategies — choose the right strategy per route