First, the HTTP status Code 304 understanding
1, if the client in the request for a file, found itself cached files have last Modified, then in the request will contain if Modified Since, this time is the cache file of the last Modified. Therefore, if the request contains if Modified Since, it indicates that there is already a cache on the client. As long as you determine the time and the modified time of the currently requested file, you can decide whether to return 304 or 200.
2, for static files, such as: CSS, images, the server will automatically complete the last Modified and If Modified Since comparison, complete the cache or update.
3, for dynamic pages, is the dynamically generated pages, often does not contain the last Modified information, so that browsers, gateways and so will not do cache, that is, every request to complete a 200 request. Therefore, for the dynamic page cache acceleration, first to add the last Modified definition in the HTTP Header of the Response, followed by the If Modified Since and the requested content update time in request to return 200 or 30 4. Although the database query has been done at the time of return 304, but can avoid the next more database query, and do not return the page content and just an HTTP Header, thereby greatly reducing the bandwidth consumption, the user's feeling is also improved.
Second, what is "last-modified"?
1, when the browser first request a URL, the server side of the return status will be 200, the content is the resources you requested, while there is a last-modified attribute to mark the last time the file was modified at the end of the service, the format is similar to this:
Last-modified:fri, May 2006 : £ ºGMT
2, the client requests this URL for the second time, according to the provisions of the HTTP protocol, the browser will send the If-modified-since header to the server, asking if the file has been modified after that time:
If-modified-since:fri, May 2006 : £ ºGMT
The server-side program obtains the value of this field first, and then compares with the last modification time of the data on the server, returns the 304 not Modified status code and then stops if the server-side resource does not change. This saves the amount of data transferred and achieves bandwidth savings. 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.
Third, why to use conditional request
When a user accesses a Web page, the conditional request accelerates the page's opening time (because it saves time to transmit the entire response body), but there is still network latency because the browser has to generate a conditional request for each resource and wait until the server returns a http/304 response. To read the cache to display the Web page. Ideally, the server specifies the Cache-control or expires directive on the response so that the client can know how long the resource is available, and can skip the steps of the conditional request and directly use the resources in the cache.
You still need to use conditional requests in the following cases
1. After the expiration time specified by the server is exceeded
2. If the user performs a refresh operation
Reference: HTTP status Code 304 http://www.studyofnet.com/news/982.html
HTTP status Code 304 What does that mean?