Amblem
Furkan Baytekin

What Is the `Cache-Control` Header?

HTTP Cache-Control header guide

What Is the `Cache-Control` Header?
81
3 minutes

If you’re building anything for the web-APIs, SPAs, static sites-your performance heavily depends on proper caching. The Cache-Control header is the core of HTTP caching. It tells browsers, CDNs, and reverse proxies exactly how to cache your response.

Let’s break it down without the noise.


What Does Cache-Control Do?

It defines caching rules:

You attach it to your response like this:

Cache-Control: max-age=3600, public

This says: β€œCache this for 1 hour. Anyone can store it.”


Common Cache-Control Directives

1. max-age

How long (in seconds) the resource should be cached.

Cache-Control: max-age=86400

1 day.


2. public vs private

Example:

Cache-Control: private, max-age=600

3. no-cache

The content can be cached, but must be revalidated before use.

Cache-Control: no-cache

It’s misleading. It doesn’t block caching. It blocks using the cached version without checking with the server.


4. no-store

The nuclear option.

Cache-Control: no-store

Nothing is cached. Useful for:


5. must-revalidate

Forces the cache to revalidate when expired.

Cache-Control: max-age=0, must-revalidate

Good for data that must always be fresh.


6. immutable

For assets that never change.

Cache-Control: max-age=31536000, immutable

Tells the browser: β€œDon’t even try revalidating. This file will never change.”

Perfect for:


Best Practices

Static Assets (JS, CSS, images)

If you use hashed filenames:

Cache-Control: public, max-age=31536000, immutable

Fastest possible web experience.


API Responses

Most APIs shouldn’t be cached globally:

Cache-Control: private, max-age=0, no-cache

For public APIs (weather, news, etc.):

Cache-Control: public, max-age=60

HTML Pages

If content changes often:

Cache-Control: no-cache

SPAs with versioned assets:

Cache-Control: no-cache, must-revalidate

Examples

Express.js

js
app.get("/api/data", (req, res) => { res.set("Cache-Control", "private, max-age=0, no-cache"); res.json({ ok: true }); });

Nginx

nginx
location /static/ { add_header Cache-Control "public, max-age=31536000, immutable"; }

Go

go
w.Header().Set("Cache-Control", "public, max-age=60")

Why Cache-Control Matters

Caching is easy to ignore but painful to fix when misconfigured.


Conclusion

Cache-Control gives you fine-grained control over how your app is cached across the web. Use it well, and your site becomes fast, cheap, and stable. Use it badly, and you’ll serve stale HTML or hammer your servers for no reason.


Album of the blog:

Suggested Blog Posts