Request Caching: cache, reload, no-cache, force-cache
You'll learn how to control whether the browser uses cached responses or fetches fresh data from the server using cache modes.
What is Request Caching?
When you use the Fetch API, the browser can store responses in its HTTP cache and reuse them for future requests. The cache option in your request configuration lets you control how the browser should interact with this cache. Think of it like giving the browser instructions: "Should I check my saved copy first, or always get the freshest version?"
The Four Main Cache Modes
default (cache)
The browser follows standard HTTP caching rules. It checks if the cached response is still fresh and uses it if valid, otherwise fetches from the network.
fetch('/api/data', { cache: 'default' })
.then(response => response.json());
reload
Always fetch from the network, ignoring any cached version. Like pressing the refresh button—you want the latest data right now.
fetch('/api/data', { cache: 'reload' })
.then(response => response.json());
no-cache
Check with the server if the cached version is still valid before using it. The browser says "I have this cached—is it still good?"
fetch('/api/data', { cache: 'no-cache' })
.then(response => response.json());
force-cache
Use the cached version if available, regardless of age. Only fetch from network if nothing is cached. Best for offline-first scenarios.
fetch('/api/data', { cache: 'force-cache' })
.then(response => response.json());
When to Use Each Mode
- default: Most common—balanced performance and freshness
- reload: Critical data that must be current
- no-cache: When you want freshness but can benefit from cache validation
- force-cache: Offline capability or when data rarely changes
Key Takeaway: Cache modes give you fine-grained control over network performance versus data freshness, letting you optimize each request based on your application's needs.