Skip to content

Service worker / offline support

A service worker is a script the browser keeps between visits. It can answer requests from a cache it filled earlier. It does nothing for a first visit, because it installs after the page has loaded. On repeat visits it can serve the built JS and CSS without a network round trip, and it can keep pages working offline. The row is ✅ when the framework builds and registers a service worker and gives it the list of built assets to precache.

14 / 14Other featureOne of 7 features that do not affect first paint. Together they are worth 14 of the 28 points.

Labels across 23 frameworks

  • 1st-party2
  • Experimental1
  • Third-party3
  • Manual17
  • of 23

How to enable it, by framework

One card per framework, in the same order on every feature page. Where a framework has no built-in support (Manual), a short example shows how to build it yourself.

Next

Third-party1 ptOverview

Next has no service worker build step. Its PWA guide hand-writes public/sw.js for push notifications and names the community Serwist package for offline caching. @serwist/next wraps the Next config, compiles your worker, and injects the precache list. Next's own experimental useOffline hook retries failed navigations once the network returns; it is not a service worker.

import withSerwistInit from "@serwist/next";

const withSerwist = withSerwistInit({
  swSrc: "app/sw.ts",
  swDest: "public/sw.js",
});

export default withSerwist({
  // Your Next.js config
});

Adapted from the Serwist getting-started guide (revision and offline-fallback entries omitted).

SvelteKit

1st-party2 ptsOverview

Create src/service-worker.js (or src/service-worker/index.ts). SvelteKit bundles it and registers it automatically, and the $service-worker module gives it the built files, the static files, and a version string.

import { build, files, version } from '$service-worker';

const CACHE = `cache-${version}`;
const ASSETS = [...build, ...files];

Adapted from the SvelteKit service workers docs (the install, activate, and fetch handlers that use these are omitted).

No service worker support is documented. Put a hand-written sw.js in public/ (styling and assets) and register it yourself; the worker has no list of built assets unless you generate one.

Code example

Put a hand-written worker in public/ and register it with an inline script in the document, next to the theme script.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/Document.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// …
        <script>{themeScript}</script>
        <script>{swScript}</script>

Source: Using Service Workers (MDN) · SolidStart styling and assets

No service worker or offline guide exists in the TanStack Start docs. Hand-write the worker and register it from the root route.

Code example

Put a hand-written worker in public/ and register it with an inline script from the root route's head.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/routes/__root.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// …
    scripts: [{ children: themeScript }, { children: swScript }],

Source: Using Service Workers (MDN) · TanStack Start document head

The same: nothing in the Solid variant's docs.

Code example

Put a hand-written worker in public/ and register it with an inline script from the root route's head.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/routes/__root.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// …
    scripts: [{ children: themeScript }, { children: swScript }],

Source: Using Service Workers (MDN) · TanStack Start document head

Pracht

Manual0 ptsOverview

No service worker support is documented in the Pracht docs. Hand-write the worker, serve it from public/, and register it from the shell.

Code example

Put a hand-written worker in public/ and register it with an inline script from the shell's head().

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/shells/public.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// … in the shell's head()
    script: [{ children: themeScript }, { children: swScript }],

Source: Using Service Workers (MDN) · Pracht shells

Waku

Manual0 ptsOverview

No service worker support is documented. Put a hand-written worker in the public folder and register it from a client component.

Code example

Put a hand-written worker in public/ and register it with an inline script in the root layout.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/pages/_layout.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// … inside RootLayout
      <script dangerouslySetInnerHTML={{ __html: swScript }} />

Source: Using Service Workers (MDN) · Waku static assets

Ember

Manual0 ptsOverview

No first-party service worker is documented in the Ember guides. The community ember-service-worker addon was last published in 2020 (9.0.1) and plugs into the classic Ember CLI build, which this Vite port does not use.

Code example

Put a hand-written worker in public/ and register it with an inline script in index.html.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
<!-- index.html, before </head> -->
<script>
  if ('serviceWorker' in navigator) {
    addEventListener('load', () => navigator.serviceWorker.register('/sw.js'));
  }
</script>

Source: Using Service Workers (MDN)

Astro

Third-party1 ptOverview

No first-party service worker documented. The community @vite-pwa/astro integration (2.0.4) generates the worker and precache manifest with Workbox and registers it; the Astro docs do not mention it. (vite-pwa astro)

Code example

Add the @vite-pwa/astro integration, which generates a Workbox worker and precache manifest at build time. Register it from the main layout.

// astro.config.mjs (npm install -D @vite-pwa/astro)
import { defineConfig } from "astro/config";
import AstroPWA from "@vite-pwa/astro";

export default defineConfig({
  integrations: [AstroPWA({ workbox: { navigateFallback: "/404" } })],
});
---
// src/layouts/Layout.astro
import { pwaInfo } from "virtual:pwa-info";
---
<head>
  <script src="/src/pwa.ts"></script>
  {pwaInfo && <Fragment set:html={pwaInfo.webManifest.linkTag} />}
</head>
// src/pwa.ts
import { registerSW } from "virtual:pwa-register";
registerSW({ immediate: true });

Source: Vite PWA: Astro

No service worker support documented. The community @vite-pwa/remix package (0.2.0, published 2025-03-30) targets Remix and does not state React Router v8 support, so it is not credited. Hand-write the worker in public/ and register it. (vite-pwa remix)

Code example

Put a hand-written worker in public/ and register it with an inline script in the root route.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// app/root.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// … in <head>
        <script dangerouslySetInnerHTML={{ __html: swScript }} />

Source: Using Service Workers (MDN)

Qwik City

Experimental1 ptOverview

@qwikdev/pwa (0.0.4, published 2024-02-26 from the QwikDev org) adds a qwikPwa() Vite plugin and a setupPwa() call in src/routes/service-worker.ts that precaches built assets; its README says it is in its early stages, and qwik.dev does not document it. Qwik's own service worker only prefetched bundles and the preloader has replaced it. (QwikDev/pwa, bundle)

Code example

@qwikdev/pwa adds a qwikPwa() Vite plugin and a setupPwa() call in the service worker route. It precaches the built q-*.js files, images and public/ assets.

// vite.config.ts (npm install -D @qwikdev/pwa)
import { qwikPwa } from '@qwikdev/pwa'
// …
plugins: [qwikCity(), qwikVite(), qwikPwa()],
// src/routes/service-worker.ts
import { setupServiceWorker } from '@builder.io/qwik-city/service-worker'
import { setupPwa } from '@qwikdev/pwa/sw'

setupServiceWorker()
setupPwa()

Source: QwikDev/pwa README

Angular (SSR)

1st-party2 ptsOverview

ng add @angular/pwa adds @angular/service-worker, registers it, and creates ngsw-config.json; the build writes the hashed asset manifest, and the worker caches the app shell and configured data groups. (getting-started, service-workers)

Code example

ng add @angular/pwa adds @angular/service-worker, registers it in the app config and creates ngsw-config.json. The build writes the asset manifest the worker uses.

// src/app/app.config.ts
import { ApplicationConfig, isDevMode } from '@angular/core';
import { provideServiceWorker } from '@angular/service-worker';

export const appConfig: ApplicationConfig = {
  providers: [
    provideServiceWorker('ngsw-worker.js', {
      enabled: !isDevMode(),
      registrationStrategy: 'registerWhenStable:30000',
    }),
  ],
};
// ngsw-config.json
{
  "index": "/index.html",
  "assetGroups": [
    { "name": "app", "installMode": "prefetch",
      "resources": { "files": ["/favicon.ico", "/index.html", "/*.css", "/*.js"] } }
  ],
  "dataGroups": [
    { "name": "api", "urls": ["/api/**"],
      "cacheConfig": { "strategy": "performance", "maxSize": 100, "maxAge": "1d" } }
  ]
}

Source: Angular: Getting started with service workers, Angular: Service worker configuration

Marko

Manual0 ptsOverview

No service worker support documented in the Marko or Marko Run docs. Hand-write the worker, serve it as a static file, and register it from a <script>. (file-based-routing)

Code example

Put a hand-written worker in public/ and register it with an inline script in the root layout.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/routes/+layout.marko
static const SW_SCRIPT = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// … in <head>
    <html-script>$!{SW_SCRIPT}</html-script>

Source: Using Service Workers (MDN)

Fresh

Manual0 ptsOverview

No service worker support documented. Put a hand-written worker in static/ and register it from an island. (static-files)

Code example

Put a hand-written worker in static/ and register it with an inline script in the app wrapper.

// static/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// routes/_app.tsx
const swScript = `if('serviceWorker' in navigator)addEventListener('load',function(){navigator.serviceWorker.register('/sw.js')});`;
// … in <head>
        <script dangerouslySetInnerHTML={{ __html: swScript }} />

Source: Using Service Workers (MDN) · Fresh static files

Analog

Manual0 ptsOverview

No service worker support documented in the Analog docs. Angular's ng add @angular/pwa "enables service worker build support in the CLI", and Analog replaces the Angular CLI build with Vite, so the ngsw.json asset manifest is not generated for you. Hand-write the worker and register it. (angular service workers, migrating)

Code example

Put a hand-written worker in public/ and register it with an inline script in index.html.

// public/sw.js
const CACHE = 'books-v1'; // change the name on each deploy

self.addEventListener('activate', (event) => {
  event.waitUntil(
    caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))),
  );
});

self.addEventListener('fetch', (event) => {
  const { request } = event;
  if (request.method !== 'GET' || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
<!-- index.html, before </head> -->
<script>
  if ('serviceWorker' in navigator) {
    addEventListener('load', () => navigator.serviceWorker.register('/sw.js'));
  }
</script>

Source: Using Service Workers (MDN)

Vike

Manual0 ptsOverview

No service worker support documented in the Vike docs or its extension list. Hand-write the worker, serve it from public/, and register it from +client.js. (static-directory, extensions)

Code example

Write the worker in public/sw.js, which Vite serves at /sw.js. Register it from +client.ts.

// public/sw.js
const CACHE = 'v1'
self.addEventListener('fetch', (event) => {
  if (event.request.method !== 'GET') return
  event.respondWith(
    caches.match(event.request).then((hit) =>
      hit ?? fetch(event.request).then((res) => {
        caches.open(CACHE).then((c) => c.put(event.request, res.clone()))
        return res
      }),
    ),
  )
})
// pages/+client.ts
if ('serviceWorker' in navigator) {
  addEventListener('load', () => navigator.serviceWorker.register('/sw.js'))
}

Source: Vike: Static directory · MDN: Using Service Workers

No service worker support documented; the public assets guide shows only a <link rel="manifest"> tag. Hand-write the worker in public/ and register it from a client component. (public-assets)

Code example

Put a hand-written worker in public/ and register it from a client component rendered on every page.

// public/sw.js
const CACHE = "app-v1"; // change the name on each deploy

self.addEventListener("fetch", (event) => {
  const { request } = event;
  if (request.method !== "GET" || new URL(request.url).origin !== location.origin) return;
  // Network first, so pages stay current. The cached copy answers when offline.
  event.respondWith(
    fetch(request)
      .then((response) => {
        const copy = response.clone();
        caches.open(CACHE).then((cache) => cache.put(request, copy));
        return response;
      })
      .catch(() => caches.match(request)),
  );
});
// src/app/components/RegisterServiceWorker.tsx
"use client";
import { useEffect } from "react";

export function RegisterServiceWorker() {
  useEffect(() => {
    if ("serviceWorker" in navigator) navigator.serviceWorker.register("/sw.js");
  }, []);
  return null;
}

Source: Using Service Workers (MDN) · RedwoodSDK: Public Assets

Remix 3

Manual0 ptsOverview

No service worker support documented in the Remix 3 guides or API reference. Hand-write the worker, serve it through the asset server or a static route, and register it from a client entry. (files-and-assets)

Code example

Write sw.js by hand, serve it from the root public/ directory with staticFiles() so its scope is /, and register it from the browser entry.

// app/router.ts
export const router = createRouter({
  middleware: [staticFiles("./public", { index: false }), render({ assets })],
});

// app/actions/public/entry.ts
import { run } from "remix/component";

let app = run({ /* loadModule, … */ });
await app.ready();

if ("serviceWorker" in navigator) {
  navigator.serviceWorker.register("/sw.js");
}

Source: Remix: Request handling, common middleware

No service worker support documented. Hand-write the worker, serve it from public/, and register it from client code. (static-assets)

Code example

Void has no service worker support. Put a hand-written worker in public/ and register it from client code.

// src/register-sw.ts, imported from a client component
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
}

Source: Void: Static assets

No service worker support documented. Hand-write the worker, serve it from public/, and register it from client code. (static-assets)

Code example

Void has no service worker support. Put a hand-written worker in public/ and register it from client code.

// src/register-sw.ts, imported from a client component
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
}

Source: Void: Static assets

No service worker support documented. Hand-write the worker, serve it from public/, and register it from client code. (static-assets)

Code example

Void has no service worker support. Put a hand-written worker in public/ and register it from client code.

// src/register-sw.ts, imported from a client component
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
}

Source: Void: Static assets

No service worker support documented. Hand-write the worker, serve it from public/, and register it from client code. (static-assets)

Code example

Void has no service worker support. Put a hand-written worker in public/ and register it from client code.

// src/register-sw.ts, imported from a client component
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
}

Source: Void: Static assets

Gotchas

  • A service worker has no effect on a first visit, and a cache-first worker can keep serving an old build. Version the cache name (SvelteKit's version) and delete old caches on activate.
  • Precaching every built file downloads routes the visitor may never open.
  • The generic vite-plugin-pwa works in any Vite build but is built around a static index.html. An SSR app still has to wire the navigation fallback by hand, so it is credited only where a framework-specific package exists (@vite-pwa/nuxt, @vite-pwa/astro).
  • Service workers need HTTPS (or localhost), and most integrations disable them in dev.