HTTP Caching


Webndsday
June 28, 2023


© David Buchmann

But... why?

HTTP: Your browser chats with the webserver

Request

GET /path
Accept-Encoding: text/html
            

Response

HTTP/1.1 200 OK
Content-Type: text/html

<html>...</html>
            

HTTP verbs

HTTP response codes

twitter.com/stevelosh/status/372740571749572610




HTTP Cache Control

Cache control headers

HTTP 1.1, RFC 2616, Sections 13.2 and 13.3

Cache Expiration

Cache-Control: s-maxage=3600, max-age=900
Expires: Thu, 15 May 2014 08:00:00 GMT
            

Cache validation

ETag: 82901821233

If-None-Match: 82901821233

304 Not Modified

Do not cache

Cache-Control: s-maxage=0, private, no-store
            

Keep variants apart

Response content depends on request headers

Requests

GET /resource
Accept: application/json
            
GET /resource
Accept: text/xml
            

Response

Vary: Accept
            

Cache busting

<link rel="stylesheet" href="/css/style.css?v1" type="text/css"/>
...
<script src="/js/scripts.js?v1"></script>
            

CDN / Reverse Proxy




Control your reverse proxy

Surrogate-Control: max-age=900, no-cache
            
Surrogate-Control: no-store
            

Check documentation of your surrogate!

Thank you!


@dbu