By karthiksudarshan on Jun 08, 2007
Browser caching has been taken for granted for so long, that I had not had an opportunity to explore it. Recently, I went through it for leveraging the cache of the browser and I want to put together the findings here :
Expiration caching -
The browser caches contents of a page (with the key as the url) for a predefined amount of time, which is indicated in the HTTP Response headers. After this time the browser sends a request to the server, to check for fresh contents.
Validation caching -
With HTTP 1.1 a new tag called the Entity Tag (ETag) is introduced, which is opaque to the browser (i.e., just stored by the browser). The browser does not do anything with the ETag but just sends the value as "If-None-Match" HTTP Request header. The server can utilize the ETag by sending a value which identifies the contents of a page, for instance, the userId so that the cache is valid for the user's session. On receiving the ETag value in the "If-None-Match" header, the server can validate whether the contents in the browser cache are still valid, for instance, now the server knows that the user has logged out, and so the cache is invalid, and the fresh contents can be sent back.
The typical steps involved are :
- The browser caches contents of a page if the HTTP Response's header indicates so. This might be through setting the "Expires" header or the new "Cache-Control: max-age=number_of_seconds" header. This provides the period till when the cache is valid.
- The browser uses this cache (without sending a request to the server) when the Back button, or the Forward button is clicked, and when accessing the History or Bookmarked url, provided the cache has not expired.
- If the Refresh button is clicked, then the browser sends a request to the server since it is an explicit request by the user to reload the page. The HTTP Request header "If-Modified-Since" will have the time period which the server can check against. The server thus has an opportunity to send fresh contents. But if the cached contents are still valid, then the server need not send any body in the response but just send a 304 status in the HTTP Response. If a 304 status is seen by the browser, the browser's cache is reused. If an ETag was sent in the original HTTP Response (ETag: exampleETagValue) then the browser sends this value as "If-None-Match" conditional header in the HTTP Request along with the "If-Modified-Since". As explained before the server can validate the cache by checking whether the ETag is still valid or not.
- If the cache has expired (due to the expiration period being
completed, or due to explicit cache clearance by the user), then the
server must send a response containing the content. For such a request, the conditional HTTP Request headers will not be present.