|
1 | 1 | # HTTP Caching
|
2 | 2 |
|
3 |
| -## Initial response |
| 3 | +## Resource that does not want to be cached |
4 | 4 |
|
5 |
| -## When server has same etag |
| 5 | +```http |
| 6 | +GET /about HTTP/1.1 |
| 7 | +HOST: example.org |
| 8 | +``` |
6 | 9 |
|
7 |
| -## When server has not been modified since |
| 10 | +```http |
| 11 | +HTTP/1.1 200 OK |
| 12 | +Cache-Control: max-age=0, private, must-revalidate |
| 13 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 14 | +Content-Type: text/html; charset=utf-8 |
| 15 | +Content-Length: 4321 |
| 16 | +``` |
8 | 17 |
|
9 |
| -## When server has different etag |
| 18 | +## Resource that wants to be cached |
10 | 19 |
|
11 |
| -## When server has been modified since |
| 20 | +```http |
| 21 | +GET /assets/logo.png HTTP/1.1 |
| 22 | +Host: example.org |
| 23 | +``` |
| 24 | + |
| 25 | +```http |
| 26 | +HTTP/1.1 200 OK |
| 27 | +ETag: "ABC" |
| 28 | +Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT |
| 29 | +Cache-Control: public, max-age=31536000 |
| 30 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 31 | +Content-Type: image/png |
| 32 | +Content-Length: 2348 |
| 33 | +Vary: Accept-Encoding |
| 34 | +
|
| 35 | +… data … |
| 36 | +``` |
| 37 | + |
| 38 | +### When server has same etag |
| 39 | + |
| 40 | +```http |
| 41 | +GET /assets/logo.png HTTP/1.1 |
| 42 | +Host: example.org |
| 43 | +If-None-Match: "ABC" |
| 44 | +``` |
| 45 | + |
| 46 | +```http |
| 47 | +HTTP/1.1 304 Not Modified |
| 48 | +ETag: "ABC" |
| 49 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 50 | +Content-Type: image/png |
| 51 | +Content-Length: 2348 |
| 52 | +Cache-Control: public, max-age=31536000 |
| 53 | +Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT |
| 54 | +Vary: Accept-Encoding |
| 55 | +
|
| 56 | +``` |
| 57 | + |
| 58 | +### When server has not been modified since |
| 59 | + |
| 60 | +```http |
| 61 | +GET /assets/logo.png HTTP/1.1 |
| 62 | +Host: example.org |
| 63 | +If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT |
| 64 | +``` |
| 65 | + |
| 66 | +```http |
| 67 | +HTTP/1.1 304 Not Modified |
| 68 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 69 | +Content-Type: image/png |
| 70 | +Content-Length: 2348 |
| 71 | +Cache-Control: public, max-age=31536000 |
| 72 | +ETag: "ABC" |
| 73 | +Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT |
| 74 | +Vary: Accept-Encoding |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +### When server does not have that etag |
| 79 | + |
| 80 | +```http |
| 81 | +GET /assets/logo.png HTTP/1.1 |
| 82 | +Host: example.org |
| 83 | +If-None-Match: "ABC" |
| 84 | +``` |
| 85 | + |
| 86 | +```http |
| 87 | +HTTP/1.1 200 OK |
| 88 | +ETag: "XYZ" |
| 89 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 90 | +Content-Type: image/png |
| 91 | +Content-Length: 2348 |
| 92 | +Cache-Control: public, max-age=31536000 |
| 93 | +Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT |
| 94 | +Vary: Accept-Encoding |
| 95 | +
|
| 96 | +``` |
| 97 | + |
| 98 | +### When server has been modified since |
| 99 | + |
| 100 | +```http |
| 101 | +GET /assets/logo.png HTTP/1.1 |
| 102 | +Host: example.org |
| 103 | +If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT |
| 104 | +``` |
| 105 | + |
| 106 | +```http |
| 107 | +HTTP/1.1 200 OK |
| 108 | +ETag: "XYZ" |
| 109 | +Date: Fri, 02 Oct 2020 09:28:50 GMT |
| 110 | +Content-Type: image/png |
| 111 | +Content-Length: 2348 |
| 112 | +Cache-Control: public, max-age=31536000 |
| 113 | +Last-Modified: Thu, 22 Oct 2015 07:28:00 GMT |
| 114 | +Vary: Accept-Encoding |
| 115 | +
|
| 116 | +``` |
| 117 | + |
| 118 | +## See more |
| 119 | + |
| 120 | +- [MDN: HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) |
| 121 | +- [MDN: If-None-Match](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match) |
| 122 | +- [MDN: 412 Precondition Failed](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412) |
0 commit comments