Course contentsShow
← Web Development
Lesson 2391 of 2,98243. Service Workers Deep DiveFree lesson

Network-Only Strategy

Implementing network-only fetches for resources that should never be cached or served stale.

Network-Only Strategy

You'll learn when and how to force your service worker to always fetch from the network, bypassing the cache entirely.

After exploring various caching strategies, you might assume every resource should be cached. Not true! Some resources should never be cached—think real-time data, authentication tokens, or highly sensitive information that must always be fresh.

The Core Idea

The Network-Only Strategy means your service worker intercepts a request but always forwards it to the network, never checking or storing it in the cache. If the network fails, the request fails—no stale fallback.

This is useful for:

  • Live data endpoints (stock prices, chat messages)
  • POST/PUT/DELETE requests (already not cached by default)
  • Tracking or analytics pixels
  • Resources where serving stale data could cause errors

Implementation Pattern

In your service worker's fetch event handler, simply return fetch(event.request) without touching the Cache API:

self.addEventListener('fetch', (event) => {
  const url = new URL(event.request.url);
  
  // Network-only for API endpoints
  if (url.pathname.startsWith('/api/realtime')) {
    event.respondWith(fetch(event.request));
    return;
  }
  
  // Other strategies for other routes...
});

You're essentially telling the browser: "I'm handling this, but just do what you'd normally do—fetch from network."

Analogy: Like calling a hotline where you always want to speak to a live operator, never accepting a recording—even if it means waiting or getting a busy signal.

Key Takeaway: Network-Only Strategy ensures certain resources always fetch fresh data from the server, explicitly avoiding any cache involvement even when a service worker is active.