|
| 1 | +# Caching with ETags |
| 2 | + |
| 3 | +_since v4.2_ |
| 4 | + |
| 5 | +GET requests return an [ETag](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) HTTP header, which can be used by the client in subsequent requests to save network bandwidth. |
| 6 | + |
| 7 | +Be aware that the returned ETag represents the entire response body (a 'resource' in HTTP terminology) for a request URL that includes the query string. |
| 8 | +This is unrelated to JSON:API resources. Therefore, we do not use ETags for optimistic concurrency. |
| 9 | + |
| 10 | +Getting a list of resources returns an ETag: |
| 11 | + |
| 12 | +```http |
| 13 | +GET /articles?sort=-lastModifiedAt HTTP/1.1 |
| 14 | +Host: localhost:5000 |
| 15 | +``` |
| 16 | + |
| 17 | +```http |
| 18 | +HTTP/1.1 200 OK |
| 19 | +Content-Type: application/vnd.api+json |
| 20 | +Server: Kestrel |
| 21 | +Transfer-Encoding: chunked |
| 22 | +ETag: "7FFF010786E2CE8FC901896E83870E00" |
| 23 | +
|
| 24 | +{ |
| 25 | + "data": [ ... ] |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +The request is later resent using the received ETag. The server data has not changed at this point. |
| 30 | + |
| 31 | +```http |
| 32 | +GET /articles?sort=-lastModifiedAt HTTP/1.1 |
| 33 | +Host: localhost:5000 |
| 34 | +If-None-Match: "7FFF010786E2CE8FC901896E83870E00" |
| 35 | +``` |
| 36 | + |
| 37 | +```http |
| 38 | +HTTP/1.1 304 Not Modified |
| 39 | +Server: Kestrel |
| 40 | +ETag: "7FFF010786E2CE8FC901896E83870E00" |
| 41 | +``` |
| 42 | + |
| 43 | +After some time, the server data has changed. |
| 44 | + |
| 45 | +```http |
| 46 | +GET /articles?sort=-lastModifiedAt HTTP/1.1 |
| 47 | +Host: localhost:5000 |
| 48 | +If-None-Match: "7FFF010786E2CE8FC901896E83870E00" |
| 49 | +``` |
| 50 | + |
| 51 | +```http |
| 52 | +HTTP/1.1 200 OK |
| 53 | +Content-Type: application/vnd.api+json |
| 54 | +Server: Kestrel |
| 55 | +Transfer-Encoding: chunked |
| 56 | +ETag: "356075D903B8FE8D9921201A7E7CD3F9" |
| 57 | +
|
| 58 | +{ |
| 59 | + "data": [ ... ] |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Note: To just poll for changes (without fetching them), send a HEAD request instead: |
| 64 | + |
| 65 | +```http |
| 66 | +HEAD /articles?sort=-lastModifiedAt HTTP/1.1 |
| 67 | +Host: localhost:5000 |
| 68 | +If-None-Match: "7FFF010786E2CE8FC901896E83870E00" |
| 69 | +``` |
| 70 | + |
| 71 | +```http |
| 72 | +HTTP/1.1 200 OK |
| 73 | +Server: Kestrel |
| 74 | +ETag: "356075D903B8FE8D9921201A7E7CD3F9" |
| 75 | +``` |
0 commit comments