Skip to content
Search

HTTP:Headers:Cache-Control

lastmod 2024-03-20created 2023-02-156 edits

The Cache-Control general header field is used to specify directives for caching mechanisms in both requests and responses. Caching directives are unidirectional, meaning that a given directive in a request does not imply that the same directive is given in the response.

Syntax

Directives are case-insensitive and have an optional argument that can use both token and quoted-string syntax. Multiple directives are separated by commas.

Cache request directives

Standard Cache-Control directives that can be used by the client in an HTTP request.

Cache-Control: max-age=<seconds>
Cache-Control: max-stale[=<seconds>]
Cache-Control: min-fresh=<seconds>
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: only-if-cached

Cache response directives

Standard Cache-Control directives that can be used by the server in an HTTP response.

Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>

Directives

Cacheability

public
Indicates that the response may be cached by any cache.
private
Indicates that the response is intended for a single user and must not be stored by a shared cache. A private cache may store the response.
no-cache
Forces caches to submit the request to the origin server for validation before showing a cached copy to the user.
only-if-cached
Indicates not to retrieve new data. The client wants only a cached response and should not contact the server to check whether a newer copy exists.

Expiration

max-age=<seconds>
Specifies the maximum amount of time a resource is considered fresh. Unlike Expires, this directive is relative to the time of the request.
s-maxage=<seconds>
Overrides max-age or the Expires header, but applies only to shared caches (such as proxies) and is ignored by private caches.
max-stale[=<seconds>]
Indicates whether the client will accept a response that has exceeded its cache expiration time. Optionally, a value in seconds can be assigned, which indicates the time during which the response must never be expired.
min-fresh=<seconds>
Indicates that the client wants a response that will remain fresh for the specified time (in seconds).
stale-while-revalidate=<seconds> Experimental
Indicates that the client will accept a stale response while asynchronously checking in the background for a fresh one. The seconds value indicates how long the client will accept a stale response.
stale-if-error=<seconds> Experimental
...

Revalidation and reloading

must-revalidate
The cache must verify the status of existing resources before using them, and expired resources must not be used.
proxy-revalidate
Same as must-revalidate, but applies only to shared caches (such as proxies) and is ignored by private caches.
immutable
Indicates that the response body will not change over time. If the response has not expired, it will not change on the server, so the client must not send a conditional revalidation for it (such as If-None-Match or If-Modified-Since) to check for updates. Clients that do not recognize this extension must ignore it according to the HTTP specification. In Firefox, immutable is only honored on https:// transactions. For more information, see the following blog post.

Other

no-store
The cache must not store anything about the client request or the server response.
no-transform
No transformations or conversions should be made to the response. The Content-Encoding, Content-Range, and Content-Type headers must not be modified by a proxy. A semi-transparent proxy might, for example, convert image formats to save cache space or reduce traffic on a slow link. The no-transform directive disallows this.

==

<meta>

Tag ==

This approach adds a meta tag in the index.html file to set the cache expiration time or to disable caching.

Example:

<!-- Expires 속성 사용: 지정한 날짜까지만 캐시 유효  -->
<meta http-equiv="Expires" content="Sun, 07 Aug 2022 18:51:54 GMT">

<!-- Expires 속성 사용: 즉시 캐시 만료 -->
<meta http-equiv="Expires" content="0">

<!-- Cache-control 속성 사용: 캐시를 하지 않음 -->
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

Examples

Preventing caching

To turn off caching, you can send the following directives. See also the Expires and Pragma headers.

Cache-Control: no-cache, no-store, must-revalidate

HTML

<head>

's

<meta>

Tag: for how to solve this there, see Cache Busting#Solution specifying cache attributes in the Meta tag.

Caching static assets

For files in the application that will not change, you can usually add aggressive caching. This includes, for example, static files served by the application such as images, CSS files, and JavaScript files. See also the Expires header.

Cache-Control:public, max-age=31536000

See also

  • HTTP
  • HTTP:Headers
  • Cache Busting - The problem where, because of leftover old caches, a newly deployed browser or images and the like are not reflected and the old cached state is still seen.
  • React:CacheBusting
  • HTTP:Headers:Expires
  • HTTP:Headers:Pragma

Favorite site

author Zer0 sitemap