1. Strong cache

Strong caching is when we access a URL, we don't send a request to the server, we read the resource directly from the cache, but we return a status code of 200. The status code returned by the HTTP request is 200 and is followed by from Memory cache or from Disk cache.Copy the code

How to set up strong cache:

The first time we go to the page, we ask the server, and the server responds, and the browser determines whether to cache the resource based on the response Header. If the response Header contains expires, pragma, or cache-Control fields, it means that this is a strong cache. The browser will cache the resource in memory cache or disk cache. On the second request, the browser determines the request parameters, returns status code 200 if it meets the strong cache criteria, and retrieves data from the local cache. Otherwise, the response parameters are stored in the Request Header to check whether they match the negotiation cache. If they match the negotiation cache, the status code 304 is returned. If they do not match the negotiation cache, the server returns a new resource.Copy the code

What does strong caching do?

1) For the resources that consume bandwidth and seldom change on the page, once downloaded, they can be directly cached in the browser. Users can use them again, which can greatly reduce the loading waiting time and improve user experience; 2) For users with weak network or unstable network, strong cache can reserve enough bandwidth for the main resource request to prevent the page from being displayed normally due to the timeout of the main resource request.Copy the code

2. Negotiate cache

Negotiation cache is a process in which the browser sends a request to the server with the cache id after the strong cache is invalid. The server decides whether to use the cache based on the cache ID.Copy the code

How to set up negotiation cache:

Settings in the Response header

Etag: '5C20ABBD-e2e8' ETAG is a unique identifier (generated by the server) that is returned to the current resource file when the server responds to a request. Last-modified: Mon, 24 Dec 2018 09:49:49 GMT Last-modified returns the time when the resource file was last modified on the server in response to a request.Copy the code