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. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.
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 missNetworkFirst
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 offlineStaleWhileRevalidate
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 backgroundNetworkOnly 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.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 42
- Lessons
- 156
Frequently Asked Questions
Is the “Service Worker Strategies” lesson free?
Yes — any 3 lessons of the Vue Academy learning path, including “Service Worker Strategies”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- vite-plugin-pwa Setup
- Service Worker Strategies
- Offline Support and Background Sync
- Push Notifications in Vue PWA