Skip to content

[http] Last-Modified, If-Modified-Since, ETag 和 If-None-Match 介紹

Published: at 08:50 AM

在上一篇介紹了  http 關於 cache 的 header ,但當 cache 過期或是需要 revalidate 時又是如何運作的呢?

此篇將介紹幾個常見的 revalidate 的 http header

Last-Modified, If-Modified-Since (RFC 7232)


request

用於做 revalidate 時,檢查檔案在指定時間後是否有被變更,若有回傳 200 及新的檔案內容,反之則回傳 304 (Not Modified) 即可。

response:

在 response 帶回此檔案上次在伺服器被修改的時間,以備做 revalidate 時發送的 timestamp。

實際上的使用情境會像是以下步驟:

  1. client 發 request 與伺服器要 image.png 檔案
  2. server 回傳 Cache-Control: max-age=86400, no-cache ,  Last-Modified: Wed, 21 Oct 2018 07:28:00 GMT 及 image.png 檔案
  3. client 收到後將 image.png 存入 cache
  4. client 下次來訪網頁,再次要 image.png,因為 no-cache 所以需要做  revalidate,因此會發送 If-Modified-Since: Wed, 21 Oct 2018 07:28:00 GMT
  5. server 看檔案在 Wed, 21 Oct 2018 07:28:00 GMT 在這之後是否有被修改
  1. client 若收到 200 則更新 cache, 反之則使用 cache 的檔案即可。

這樣就結束了 revalidate,可以看出這樣既檢查檔案修改時間也可以省下許多檔案(body)的流量。

優點

缺點

ETag, If-None-match (RFC 7232)


request:

用於做 revalidate 時,檢查內容的 hash 值是否有更改,若有回傳 200 及新的檔案內容,反之則回傳 304 (Not Modified) 即可。

response:

在 response 帶回此檔案 ETag 值,以備做 revalidate 時發送的 timestamp。

實際上的使用情境會像是以下步驟:

  1. client 發 request 與伺服器要 image.png 檔案
  2. server 回傳 Cache-Control: max-age=86400, no-cache ,  ETag: “33a64df551425fcc55e4d42a148795d9f25f89d4” 及 image.png 檔案
  3. client 收到後將 image.png 存入 cache
  4. client 下次來訪網頁,再次要 image.png,因為 no-cache 所以需要做  revalidate,因此會發送 If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"
  5. server 計算此檔案的 ETag 值是否有更改
  1. client 若收到 200 則更新 cache, 反之則使用 cache 的檔案即可。

這樣就結束了 revalidate,可以看出這樣既可以檢查檔案內容也可以省下許多檔案(body)的流量。

優點

缺點

參考資料:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag

https://tools.ietf.org/html/rfc7232#section-2.2

https://tools.ietf.org/html/rfc7232#section-2.3