Course contentsShow
← JavaScript
Lesson 2273 of 2,72347. Networking PrimitivesFree lesson

Request Cache Control

Use the cache property to specify caching behavior: default, no-store, reload, no-cache, force-cache, only-if-cached.

Request Cache Control

You'll learn how to control whether the browser uses cached responses or always fetches fresh data from the network.

What is Cache Control?

When you make a fetch request, the browser can retrieve the response from its cache instead of hitting the network every time. The cache property on a Request lets you specify exactly how you want caching to behave.

Think of it like ordering coffee: sometimes you're fine with what's already brewed (default), sometimes you need a fresh pot made just for you (reload), and sometimes you want to check if the existing pot is still good before deciding (no-cache).

The Cache Options

The cache property accepts these values:

default – Use the browser's standard HTTP cache behavior. If there's a fresh cached response, use it.

no-store – Never cache the request or response. Always fetch fresh and don't save anything.

reload – Always fetch from the network, ignoring the cache, but update the cache with the new response.

no-cache – Check with the server first to see if the cached version is still valid before using it.

force-cache – Use the cached response even if it's stale, only hitting the network if nothing is cached.

only-if-cached – Only use cached responses, fail if nothing is cached (requires mode: 'same-origin').

// Force a fresh fetch, ignoring cache
fetch('https://api.example.com/data', { 
  cache: 'no-store' 
});

// Use cache if available, even if stale
fetch('/api/static-content', { 
  cache: 'force-cache' 
});

Key Takeaway: The cache property gives you fine-grained control over whether fetch uses cached responses or always retrieves fresh data from the network, letting you balance performance with data freshness.