Service Worker Recipes - Interactive Lab | IconVault
Copy-ready service worker recipes: cache-first, network-first, stale-while-revalidate plus lifecycle code. Free.
Service Worker Recipes
Cache-first, network-first, stale-while-revalidate
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
- 1Page requests /logo.svg
- 2Service worker checks the cache first
- 3Cache hit: respond instantly, zero network
- 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
More tools
Algorithm Visualizer
Watch sorting and searching animate step by step
Alt Text Helper
Write better image alt text with live feedback
ARIA Reference
Searchable ARIA roles and attributes guide
Array Visualizer
Step through map, filter, reduce like a debugger
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