Why Caching
- make non-first website loading faster
- save money for website owner
How http handle caching
Before we dig into details, lets go through how caching act in the GET request flow.
there are two steps of caching validation.
-
Local validation of caching (by priority)
- Cache-Control, according to “no-cache”, “no-store”, “max-age” and other options
- Expires, with HTTP-date like Expires: Thu, 01 Dec 1994 16:00:00 GMT
Use Cache-Control and Expires together to make more support from all browsers.
-
Remote validation after local validation finished without a decision of using cache.
- If-Modified-Since: Http-date
- Last-Modified: Http-date
- If-None-Match: version
- ETag: hashed version
Rules of using cache control
-
Static Resource
For picture, js, and css files, use versioning strategy like
base.css?s3xD5jx
Then set HTTP headers like:1. Cache-Control: public, max-age=31536000 2. Expires: Thu, 15 Oct 2017 16:00:00 GMT //a year later 3. ETag: "529eb7216432b69370103d6bdd5724dcb4" 4. Last-Modified: Thu, 07 Jul 2016 22:01:41 GMT 5. Vary: Accept-Encoding
-
Dynamic Resource
Usually, no caching for dynamic Resource
1. Cache-Control: public, max-age=0 2. Expires: Thu, 15 Oct 2016 16:00:00 GMT 3. ETag: "529eb7216432b69370103d6bdd5724dcb4" 4. Last-Modified: Thu, 07 Jul 2016 22:01:41 GMT 5. Vary: Accept-Encoding
references:
- https://bryantsai.com/http-caching/
- https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html //