Start a project

Tool 95 of 194 · Performance

Service Worker Cache Strategy Builder

Choose how each kind of request should be cached and get a working sw.js, with no library to add.

Service Worker Cache Strategy Builder

Routes

Patterns: navigate for page loads, a path such as /assets/*, an extension such as *.png, or a full URL such as https://cdn.example.com/*. Strategies: cache-first, network-first, stale-while-revalidate, network-only, cache-only.

What each route does

PatternStrategyCacheLimitsBehaviour

    sw.js

    Service worker

    Register it

    Add to your pages

    No libraries: plain Cache Storage and fetch. The generated worker was tested in Chromium against a server that goes offline and slows down, covering every strategy, the timeout, expiry, the offline page and version upgrades.

    01

    What it does

    Writes a dependency-free service worker from your routes: cache-first, network-first, stale-while-revalidate and more, with precaching, expiry, an offline page and versioning.

    • Five strategies: cache-first, network-first with a timeout, stale-while-revalidate, network-only and cache-only
    • Routes by page load, path, file extension or full URL, checked in order
    • Max entries and max age per cache
    • Precache list and an offline page for navigations

    02

    Private by design

    Everything happens in your browser. Nothing you type, paste or upload is sent anywhere, and it keeps working offline once the page has loaded.

    03

    What you get

    A complete sw.js, a registration snippet, a route table and notes, with a Copy and a Download button on every result.

    • Service workers
    • Caching
    • Offline

    Why it matters

    A service worker sits between your pages and the network, so it decides what loads from the cache and what goes to the server. The right choice depends on the request: versioned assets can come from the cache forever, pages should be fresh but still work offline, and account or checkout pages should never be cached.

    This builder turns a short list of routes into a complete sw.js using only the browser's Cache Storage and fetch APIs. Each route gets a strategy and limits, precached files are stored on install, a version number keeps caches separate, and an offline page covers navigations that fail. The table explains what every route will do.

    How to use it

    1. List your routes in order, most specific first.
    2. Add the files to precache and an offline page.
    3. Set the version, then download sw.js and serve it from your site's root.
    4. Add the registration snippet to your pages.

    FAQ

    Which strategy should I use?

    Cache-first for files whose URL changes when their content does, such as hashed assets and fonts. Network-first for HTML and API data. Stale-while-revalidate for images and data where a slightly old copy is fine. Network-only for anything private.

    Why does the order of routes matter?

    The first route that matches wins. Put network-only routes for account or checkout pages above the general navigate route, or those pages will be cached.

    How do I update the service worker?

    Change the version and deploy the new sw.js. Browsers check for a new worker on navigation; once it activates, caches from older versions with your prefix are deleted. Serve sw.js with Cache-Control: no-cache.

    Do I need Workbox?

    No. This sw.js uses the browser APIs directly. Workbox adds more options, such as background sync and broadcast updates, if you need them later.

    Work with me

    Want a site that gets these right?

    I build fast, accessible websites and web apps that pass these checks from day one. Tell me about your project and I'll reply personally with honest next steps.

    All developer tools · Back to leej.uk