🔄 Ajax: Fix a Fetch Request Caching a Response Despite No-Store
Setting cache: 'no-store' on a fetch() call controls how that request interacts with the browser’s HTTP cache, but it has no effect on a registered Service Worker’s own fetch event handler. If the Service Worker intercepts the request and serves a cached response from its own Cache Storage before the request would even reach the network layer where no-store takes effect, the page keeps seeing stale data regardless of the fetch option.
🔎 The Problem
// page code
const res = await fetch('/api/prices', { cache: 'no-store' });
// still returns a stale cached response
// service-worker.js
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((cached) => cached || fetch(event.request))
// caches.match() runs BEFORE the network layer ever sees
// the request's cache: 'no-store' option
);
});
✅ Fix: Check the Service Worker Before Blaming the HTTP Cache
- Check whether a Service Worker is registered and intercepting requests to that endpoint (Application tab > Service Workers in DevTools) before assuming a stale response is an HTTP-cache problem that
no-storeshould have fixed. - If the Service Worker’s strategy should bypass cache for a specific request, add a distinguishing marker (a query param, a custom header, or a URL pattern check) in its fetch handler so it explicitly skips
caches.match()for that case. - For genuinely cache-busting calls from the page, append a cache-busting query parameter in addition to
no-store, since that changes the request URL the Service Worker sees and can avoid matching an existing cache entry.
🚨 Why This Is Dangerous
- Debugging a “stale data” issue by only inspecting network headers and fetch options can burn significant time when the actual culprit is a layer that runs before the network request is ever made, and the fix that seems obvious does nothing at all.
no-store tells the browser’s HTTP cache to stay out of the way — it says nothing to a Service Worker that already decided to answer the request itself.
