0Pricing
Vue Academy · Lesson

Service Worker Strategies

Cache-first, network-first, stale-while-revalidate — choosing per resource type.

Service Worker Strategies is a free Vue Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Caching Strategies Overview

A service worker intercepts network requests and decides whether to serve from cache, network, or both. Workbox names four core strategies, each suited to a different kind of resource.

CacheFirst

CacheFirst serves from the cache and only hits the network on a miss. Best for assets that rarely change and are content-hashed, like fonts and images.

// serve cached, fall back to network on miss

NetworkFirst

NetworkFirst tries the network first and falls back to cache when offline or on timeout. Best for API responses where freshness matters but offline access is desired.

// try network, fall back to cache when offline

StaleWhileRevalidate

StaleWhileRevalidate returns the cached copy immediately for speed while fetching a fresh copy in the background to update the cache. Great for JS bundles and CSS.

// return cache now, update cache in background

NetworkOnly and CacheOnly

The two extremes: NetworkOnly always fetches (no caching), and CacheOnly only ever serves precached content. Use these for non-cacheable POSTs or strictly precached assets.

The runtimeCaching Array

In Workbox config, runtimeCaching is an array of rules. Each rule matches request URLs and assigns a strategy and cache name.

VitePWA({
  workbox: {
    runtimeCaching: [ /* rules */ ]
  }
});

A urlPattern Rule

Each rule has a urlPattern (regex or function), a handler (the strategy name), and options such as cache name.

{
  urlPattern: /\/api\//,
  handler: "NetworkFirst",
  options: { cacheName: "api-cache" }
}

Caching Static Assets CacheFirst

Match images and fonts and serve them CacheFirst since their content-hashed URLs change when the file changes.

{
  urlPattern: /\.(?:png|jpg|svg|woff2)$/,
  handler: "CacheFirst",
  options: { cacheName: "static-assets" }
}

Caching JS Bundles StaleWhileRevalidate

Serve script and style bundles instantly from cache while refreshing in the background.

{
  urlPattern: /\.(?:js|css)$/,
  handler: "StaleWhileRevalidate",
  options: { cacheName: "assets" }
}

The Expiration Plugin

Caches grow unbounded without limits. The expiration option caps the number of entries and their max age, evicting the oldest first.

options: {
  cacheName: "api-cache",
  expiration: {
    maxEntries: 50,
    maxAgeSeconds: 60 * 60 * 24
  }
}

Caching Opaque Responses

For cross-origin API responses you often add cacheableResponse with allowed statuses so only successful responses are stored.

options: {
  cacheableResponse: { statuses: [0, 200] }
}

Quick Check

Which strategy best fits API responses that should be fresh but available offline?

Recap

Workbox strategies map to resource types: CacheFirst for static assets, NetworkFirst for API responses, and StaleWhileRevalidate for JS/CSS bundles. Define them in the runtimeCaching array with a urlPattern, handler, and options like the expiration plugin to cap cache size.

Frequently asked questions

Is the “Service Worker Strategies” lesson free?

Yes — the full text of “Service Worker Strategies” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “Service Worker Strategies”?

Cache-first, network-first, stale-while-revalidate — choosing per resource type. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Service Worker Strategies” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. vite-plugin-pwa Setup
  2. Service Worker Strategies
  3. Offline Support and Background Sync
  4. Push Notifications in Vue PWA
← Back to Vue Academy