Service Worker Recipes - Interactive Lab | IconVault

Copy-ready service worker recipes: cache-first, network-first, stale-while-revalidate plus lifecycle code. Free.

All tools

Service Worker Recipes

Cache-first, network-first, stale-while-revalidate

5 of 5 free uses left

One honest requirement

Service workers only register over HTTPS or localhost, and the file must be a real same-origin URL (a Blob URL will not work). So this page cannot register a demo worker for you. Instead you get complete, working recipes: download the sw.js, drop it at your site root, and register it with the snippet below.

Request flow

  1. 1Page requests /logo.svg
  2. 2Service worker checks the cache first
  3. 3Cache hit: respond instantly, zero network
  4. 4Cache miss: fetch from network, store a copy, then respond

Trade-offs

Wins

  • Instant repeat loads
  • Works fully offline after first visit

Costs

  • Stale content until the cache version changes
  • Cache grows unless you version it

Best for: Static assets: images, fonts, hashed JS/CSS bundles.

sw-cache-first.js

// sw-cache-first.js - serve from cache, fall back to network.
// Best for: images, fonts, versioned bundles (app.[hash].js).

const CACHE = "static-v1";

self.addEventListener("install", (e) => {
  e.waitUntil(
    caches.open(CACHE).then((cache) =>
      cache.addAll(["/", "/styles.css", "/app.js", "/logo.svg"])
    )
  );
});

self.addEventListener("fetch", (e) => {
  e.respondWith(
    caches.match(e.request).then((hit) => {
      if (hit) return hit;                       // cache hit: instant
      return fetch(e.request).then((res) => {    // miss: go to network
        const copy = res.clone();
        caches.open(CACHE).then((cache) => cache.put(e.request, copy));
        return res;
      });
    })
  );
});

5 of 5 free uses left.

Registration snippet

// Register from your page (HTTPS or localhost only)
if ("serviceWorker" in navigator) {
  window.addEventListener("load", () => {
    navigator.serviceWorker.register("/sw.js", { scope: "/" });
  });
}

Serve the recipe as /sw.js at your site root so its scope covers the whole site. Debug in DevTools under Application and Service Workers.

About this tool

IconVault's Service Worker Recipes gives you production-ready service worker code for the three core caching strategies: cache-first, network-first and stale-while-revalidate, plus the install and activate lifecycle that versions and cleans caches. Each recipe includes a step-by-step request flow, honest trade-offs and a registration snippet. It is free to use and runs fully in your browser.

How to use: pick a recipe, study its flow, then copy or download the sw.js file and serve it from your site root. Perfect for developers adding offline support and fast repeat loads to any site or PWA.

Frequently asked questions

Popular searches

service workerservice worker exampleservice worker tutorialcache first strategynetwork first strategystale while revalidateservice worker cache apicaches.matchcache.addallservice worker installservice worker activateskipwaitingclients.claimservice worker lifecyclepwa service workeroffline firstservice worker httpsregister service workerservice worker scopeservice worker updatecache versioningdelete old cachesservice worker fetch eventrespondwithservice worker strategiescache then networknetwork only strategycache only strategyoffline fallback pageservice worker debugservice worker devtoolslearn service workersservice worker mdnworkbox vs vanillaservice worker best practicesservice worker browser supportservice worker localhostpwa offlineservice worker recipessw.js exampleservice worker codecaching strategies webhttp cache vs service workerservice worker interviewprogressive web app tutorialpwa caching guideservice worker pitfallsservice worker playgroundservice worker labservice worker fetch handler examples

More tools

Coming soon

CLI

Coming soon

Figma plugin

Coming soon

VS Code extension

Coming soon

Chrome extension

Coming soon

AI icon generator

Coming soon

Team workspaces

Coming soon

View all 10+ tools →