Offline Content Storage and Sync with Server when Modified

What is If-Modified-Since?

  • The smart and efficient way to get images, icons and more from your server.
  • If-Modified-Since is an HTTP header Which is sent to the server as a condition.
  • If the content has been changed on the server, then the server responds with 200 status code with the whole requested document.
  • Else If the content is not changed on the server, the server responds with a status code 304.

How it works?

  • When our conditional get request is made by the client to the server for a particular source, the client will provide if modified since header with the Last-Modified date of its cached copy.
  • On the basis of If-Modified-Since header server determines if requested client’s cached copy is the most updated or not.
  • If yes, then the server responds client an HTTP status code 304 i.e client can reuse its cached file.
  • If the client does not have a cached copy (i.e it is the first time visiting the resource), the resource must be downloaded from the origin server, resulting in an HTTP code 200. Similarly, this code will also be returned for requests that have an outdated version of a resource stored in cache thus requiring the latest resource to be fetched from the server.

How to implement in your code?

  1. check if data updated on server or not.
  2. pass If-Modified-Since header with time client first downloaded a resource from the server.
  3. get response code from the server.
  4. if the response code is 200, means data is updated, get data from server and store locally.
    Also, store last modified time value from header field i.e Last-Modified, this value is passed further in If-Modified-Since header.
  5. else if the response code is 304, get data from your local storage.

 

Leave a Reply