Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that a web browser check if a resource is modified/expired within a given timestamp to decide if downloading such resource or not.

So, here come two questions:

i) generally speaking how exactly this mechanism works in background, which requests/reponse are made by browser and server

ii) I have built a simple REST php web service that serves a json file by means of GET request. And GET is actually the only protocol implemented. In the response I am correctly including all the necessary header file. Is there something server side I need to implement for making my web service compliant to such 'check for last update therefore serve or not the resource' mechanism ? In particular my client is an iOS app, which by default is strictly following the http caching rule.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

HTTP supports several forms of cache control1:

Server-side

The server can specify the headers Cache-Control, ETag, Expires and Last-Modified.

  • Cache-Control contains general instructions, i.e., if, how and until when the resource may be cached.

  • ETag is an arbitrary but fixed string that the server will change if the resource changes.

  • Expires indicates until when the client doesn't have to check for a new version of the resource.

    This is commonly used in combination with cache breakers. If you address script.js as script.js?versionX (JavaScript files will ignore the query), you can set an arbitrary expiration date. If the script ever changes, you can just address it as script.js?versionY.

  • Last-Modified indicates the last modification date of the resource.

Client-side

Once the client has cached a certain resource using any of the above methods, it can proceed as follows:

  • If the resource hasn't expired yet (Cache-Control and Expires are relevant), do not request it.

  • Send the GET request with an If-Modified-Since and/or If-None-Match, indicating the ETag and/or Last-Modified header that was received when the resource was cached.

    Depending on those headers, the server may send the HTTP status code 304, indicating that the resource has not been modified. In this case, the client has to read the resource from the cache.

1These are the methods I personally know of. There may be more.

share|improve this answer
    
thanks, so when a GET request come in, I need to analyze if the if-modified, E-Tag and the rest are present in such request, and in case I can match with my criteria and answer with a 304 or with the full resource and 200 status code ? –  Leonardo Mar 5 '13 at 21:45
    
All of these are optional. If you send ETag, analyze If-None-Match. If you send Last-Modified, analyze If-Modified-Since. My personal favorite is Expires plus cache breaker. That way, the client won't make any requests, and you don't have to analyze anything. –  Dennis Mar 5 '13 at 21:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.