Swoff Swoff

Svelte + Vite

Set up Swoff with Svelte 5 + Vite.

Svelte + Vite

Concise adapter for Svelte 5 + Vite projects.

Create Project

npm create vite@latest my-app -- --template svelte-ts
cd my-app && npm install

Project Structure

my-app
├── src
│   └── main.ts
├── swoff
│   ├── sw-template.js
│   ├── sw-generator.js
│   ├── sw-injector.js
│   └── swoff.d.ts
└── package.json

Entry Point (src/main.ts)

import App from "./App.svelte";
import { initServiceWorker } from "../swoff/sw-injector";

initServiceWorker();

const app = new App({
  target: document.getElementById("app"),
});

export default app;

Add Patterns

Copy from Patterns:

  1. SW Templateswoff/sw-template.js
  2. Build Scriptswoff/sw-generator.js (output dir: dist/)
  3. Client Registrationswoff/sw-injector.js
  4. TypeScript Declarationsswoff/swoff.d.ts

Update package.json:

{
  "scripts": {
    "build": "vite build && node swoff/sw-generator.js"
  }
}

Output Directory

Vite outputs to dist/ by default. Your swoff/sw-generator.js should write to dist/.

Build & Test

npm version patch
npm run build

Next Steps

On this page