My question is pretty simple. Although while searching over, I have not found a simple satisfying answer.
I am using Jquery ajax request to get the data from a server. Server hosts a rest API that sets the Etag and Cach-control headers to the
GET requests. The Server also sets CORS headers to allow theEtag. The client of the Api is a browser web app. I am using Ajax request to call the Api. Here are the response headers from server after a simple GET request:
Status Code: 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-transform, max-age=86400
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: application/json
Date: Sun, 30 Aug 2015 13:23:41 GMT
Etag: "-783704964"
Keep-Alive: timeout=15, max=99
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Vary: Accept-Encoding
access-control-allow-headers: X-Requested-With, Content-Type, Etag,Authorization
access-control-allow-methods: GET, POST, DELETE, PUT
All I want to know is:
Do I need to manually collect the Etag from response headers sent from the server and attach an if-no-match header to ajax request?OR the Browser sends it by-default in a conditional get request when it has an 'Etag'
I have done debugging over the network console in the browser and It seems the browser is doing the
conditional GETautomatically and sets theif-no-matchheader.if it is right, Suppose, I created a new resource, and then I called the get request. It gives me the past cached data for the first time. But when I reload the page, It gives the updated one. So I am confused that, If the dataset on the server-side has changed and it sends a different
Etag, Why doesn't the browser get an updated data set from the server unless I have to reload
- Also in case of pagination. Suppose I have a URL
/users?next=0.next is a query paramwhere the value for thenextchanges for every new request. Since each response will get its own 'Etag'. Will the browser store the 'Etag' based on request or it just stores the lastestEtagof the previous get request, irrespective of the URL.
Well, I have somehow figured out the solution myself:
The browser sends the
if-no-matchheader itself when it sees url had thee-tagheader on a previousrequest. Browser saves thee-tagwith respect to that URL, so it does not matter how many requests with different URLs happen.Also, a trick to force the browser to fetch a
conditional-getto check thee-tag:Set the max-age header to the lowest(for me 60s works great)cacheexpires, thebrowser will send aconditional-getto check if theexpired cached resourceis valid. If theif-no-matchheader matches withe-tag. The server sends the response back with304: Not-Modifiedheader. This means the expired cached resource is valid and can be used.