×

注意!页面内容来自https://shayy.org/posts/cache-control/,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

Cache-Control: Cheat Sheet.

5 min read

The Cache-Control header sets rules for how web pages are stored in browser and CDN caches. It tells them how to save pages and when to check if the pages need to be updated from the original server.

   Client <------ CDN <------ Server
                              
[Web Browser] [Shared Cache] [Origin Server]

Directives:

  • no-store: The browserCDNand server are disallowed from storing any version of the returned response.
   Client <------ CDN <------ Server
                  
  • no-cache: The browser or CDN can store the response but must revalidate with the origin before serving it.
   Client <------ CDN <------ Server
                  
  Revalidate   Revalidate
  • private: The response is intended only for a single user (browser) and must not be stored by a shared cache (CDN).
   Client <------ CDN <------ Server
                  
  • public: The response may be stored by any cache including the browser and CDN.
   Client <------ CDN <------ Server
                  
  • max-age=<seconds>: Specifies the max amount of time a resource is considered fresh by the browser or CDN.
   Client <------ CDN <------ Server
   max-age      max-age
  • s-maxage=<seconds>: Similar to max-age but applies only to shared caches (CDN).
   Client <------ CDN <------ Server
               s-maxage
  • must-revalidate: Once a resource becomes stalecaches (browser or CDN) must not use their stale copy without successful validation with the server.
   Client <------ CDN <------ Server
                  
  Revalidate   Revalidate
  • proxy-revalidate: Similar to must-revalidate but does not apply to private user agent caches (browser).
   Client <------ CDN <------ Server
                   
               Revalidate
  • no-transform: The browserCDNor any intermediary must not transform the payload of the response.
   Client <------ CDN <------ Server
                  
  Transform    Transform
  • stale-while-revalidate=<seconds>: The browser or CDN can serve a stale response while it revalidates in the background.
   Client <------ CDN <------ Server
                  
 Serve Stale  Serve Stale
              Revalidate
  • stale-if-error=<seconds>: The browser or CDN can serve a stale response if the server is unavailable.
   Client <------ CDN <------ Server
                  
 Serve Stale  Serve Stale
              Error Occurs

Note: Directives can be combined in a single Cache-Control header and they are case-insensitive.

Examples:

  1. Public Cache with 1 day max age

    • The response can be stored by any cache (browser or CDN) and is considered fresh for 1 day.

    Cache-Control: publicmax-age=86400

  2. Private Cache with 1-hour max age

    • The response is intended for a single user and can be stored by the browserbut not shared caches like CDNs. It’s considered fresh for 1 hour.

    Cache-Control: privatemax-age=3600

  3. No Cache

    • The response can be stored by the browser or CDNbut they must revalidate with the server before serving it.

    Cache-Control: no-cache

  4. No Store

    • The response must not be stored by any cache (browserCDNor server).

    Cache-Control: no-store

  5. Fresh for 1 hourrevalidate after that

    • The response is considered fresh for 1 hourafter which the cache must revalidate with the server before serving it.

    Cache-Control: max-age=3600must-revalidate

  6. Public Cacheno transformation

    • The response can be stored by any cache (browser or CDN)but it must not be transformed by any intermediary.

    Cache-Control: publicno-transform

  7. Shared Cache with 2 hours max-ageprivate cache with 1-hour max age

    • The response can be stored by a shared cache (CDN) and is considered fresh for 2 hours. For private caches (browser)it’s considered fresh for 1 hour.

    Cache-Control: s-maxage=7200max-age=3600

  8. Stale while revalidating for 1 hour

    • The browser or CDN can serve a stale response while it revalidates with the origin in the background for up to 1 hour.

    Cache-Control: stale-while-revalidate=3600

  9. Stale while revalidating for 1 hourserve stale on error for 2 hours

    • The browser or CDN can serve a stale response while it revalidates with the origin in the background for up to 1 hour. If an error occurs during revalidationthe stale response can still be served for an additional 2 hours.

    Cache-Control: stale-while-revalidate=3600stale-if-error=7200

Express Example:

The code snippet below sets caching rules for the response. It allows any cache to store the response (public). The response is considered current for 24 hours when cached by a browser (max-age=86400)and for 2 hours when stored by shared cachessuch as CDNs (s-maxage=7200).

Additionallyif the response is outdatedboth the browser and CDN can still temporarily deliver the old response while they update their cache with a new onefor up to 1 hour (stale-while-revalidate=3600).

import express from 'express';
const app = express();

app.get('/', (req, res) => {
  res.set(
    'Cache-Control',
    'public, max-age=86400, s-maxage=7200, stale-while-revalidate=3600'
  );

  res.send('Hello World');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

more posts

back