Standard

Service Worker Boilerplate Generator

Generate a safe, working service-worker.js using Cache First, Network First or Stale While Revalidate, with an optional offline page and the code to register it.

Caching strategy

Generated service-worker.js


    

Add this to your pages to register it


    
Advertisement

About this tool

The generated worker handles install (downloads and stores your listed files), activate (deletes caches from older versions) and fetch (answers requests with the strategy you picked). Cache First serves stored copies when it has them, which suits versioned assets such as fonts and images. Network First always tries the internet and falls back to the cache when offline, which suits pages and data that change. Stale While Revalidate answers instantly from the cache and refreshes it in the background.

The code avoids the mistakes common in copy-and-paste examples: only successful, same-origin GET responses are cached, so error pages, form submissions and other sites' files never get stored, and add an offline page to show visitors when a page isn't available. Serve the file from your site's root (so it controls every page), over HTTPS, and change the cache name whenever you change the file list so returning visitors get the new versions. Every URL in the list must exist, or installation fails.