service-worker.js Generator
Generate a basic service worker with cache-first or network-first strategy and a precache asset list.
service-worker.js:
—
Offline caching without pulling in Workbox
You want the page to open with no network, but writing a service worker from scratch means getting three events and the lifecycle between them right. Pick a strategy (cache-first or network-first), name a cache version, and list the precache files one per line. The output is a complete service-worker.js with install, an activate that deletes stale caches, and a fetch handler that already skips non-GET requests.
Install uses addAll, which is all or nothing: if a single path in the list returns 404, the whole installation fails and the worker never activates. That is by far the most common reason a service worker refuses to register. Worth knowing too: neither strategy writes new responses into the cache while you browse, only what was in the list. That is what the version field is for, since bumping v1 to v2 makes activate drop the old cache.
Register it with navigator.serviceWorker.register and serve the file from the site root, because scope follows the directory it lives in: at /js/ it only controls /js/. HTTPS is required, with localhost as the exception. The generated code calls skipWaiting and clients.claim, so a new version takes over immediately while the open tab still runs the old JavaScript. Drop those two lines if you prefer the swap on next load.
Frequently asked questions
Cache-first or network-first, which one?
Why does my site not update after a deploy?
Does network-first work offline?
Related Tools
PWA Favicon SVG Generator
Generate a simple inline SVG favicon from letter(s) or emoji and background color. Ready to paste into <head>.
PWA manifest.json Generator
Build a manifest.json for a PWA (name, short_name, theme_color, background_color, display, icons) with preview.
Cache-Control max-age Explained
Convert the Cache-Control max-age value (in seconds) into a readable time in hours and days. Useful for understanding and debugging HTTP cache and CDNs.