Cache-Only Strategy
What you'll learn: How to serve resources exclusively from the cache with zero network interaction, and when this extreme offline-first approach makes sense.
What Is Cache-Only?
The Cache-Only strategy is the most aggressive offline-first caching pattern. When a request matches your service worker's scope, it only checks the cache—it never hits the network at all. If the resource exists in cache, you serve it. If it doesn't exist, the request fails immediately.
Think of it like a museum exhibition with no outside connections. Visitors can only see what's already inside the building. If an artifact isn't in the collection, it simply doesn't exist for them—there's no mechanism to fetch it from elsewhere.
When to Use Cache-Only
This strategy works perfectly for:
- Critical precached assets that you know will always be in cache (app shell, offline page)
- Static resources that never change once deployed with a version hash
- Offline-only features where network access would be incorrect or harmful
- Assets served with immutable URLs (e.g.,
/static/app.v2.css)
Implementation Pattern
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
// Cache-Only for versioned static assets
if (url.pathname.startsWith('/static/')) {
event.respondWith(
caches.match(event.request)
// No fallback to fetch() - cache or nothing
);
}
});
If caches.match() returns undefined, the request simply fails—no network fallback, no alternative. This is intentional: you're guaranteeing these resources are precached during the install event.
Why Use It?
Cache-Only provides maximum speed and reliability. There's zero network latency, no server downtime can affect it, and you have complete control over what users receive.
Key Takeaway: Cache-Only serves resources exclusively from cache without any network fallback—perfect for precached, immutable assets where you guarantee cache availability and want zero network overhead.