Basic knowledge
1) What is "last-modified"?
When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the end of the service period, similar in format:
Last-modified:fri, 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:
If-modified-since:fri, 18:53:33 GMT
If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.
2) What is an "Etag"?
The HTTP protocol specification defines the etag as the entity value of the requested variable. Another argument is that the ETag is a token that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for judging what the token is and what it means, and transmitting it to the client in the HTTP response header, which is the format returned by the server side:
ETag: "50b1c1d4f775c61:df3"
The client's query update format is this:
if-none-match:w/"50b1c1d4f775c61:df3"
If the etag does not change, it returns the status 304 and does not return, which is the same as last-modified. I test the etag is mainly in the breakpoint download is more useful.
how do last-modified and etags help improve performance?
Smart developers will use the HTTP header for Last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates the LAST-MODIFIED/ETAG tag, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate its (client) cache by passing the token back to the server.
The process is as follows:
The client requests a page (a).
The server returns page A and adds a last-modified/etag to a.
The client presents the page and caches the page along with Last-modified/etag.
The customer requests page A again and passes the Last-modified/etag that the server returned when the last request was sent to the server.
The server checks the last-modified or ETag and determines that the page has not been modified since the last client request, directly returning the response 304 and an empty response body.
Implementation of shelf life
In HTTP, the expiration time is specified for the document through the Cache-control header and the expires header, and by judging the expiration time, the cache will know if the document is within the shelf life. Expires header and Cache-control:max-age header are all to tell the cache document has not expired, why need two response header to do this simple thing? In fact, this is all a historical reason, expires header is proposed in HTTP 1.0, because he is using an absolute date, if the server and the client clock is not synchronized (in fact, this is very common), the cache may think that the document has expired.
HTTP 1.1 In order to fix this problem, introduced the Cache-control:max-age header, the first use of relative time to control the shelf life, so that everything becomes more reasonable. For example, we bought a bottle of soda, if we use the Expires header to mark the shelf life, it will be said: Drink expiry date: December 21, 2012, if a 2 goods do not know what number today, he did not know that the drink expired, I was a child drink so write. Later, there is a very famous selling milk, probably called Mengniu, he invented a way to label the shelf life, how did he do? He wrote: The shelf life: 12 months, line, the hell, I milk a year ago on the production of milk, today to be issued to manufacturers, the first to print on the packaging on the date of production (of course, printed on the day of shipment), and then tell you, next year only expires, this much clever, make a relative, poison you. Perhaps HTTP 1.1 draws on this great invention, so it has the cache-control:max-age header.
HTTP1.1 304 static Resource cache design