Reputation: 1114
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 GET
automatically and
sets the if-no-match
header.
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
/users?next=0
. next is a query param
where the value for the next
changes 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 lastest Etag
of the previous get request, irrespective of the URL.Upvotes: 4
Views: 1723
Reputation: 1114
Well, I have somehow figured out the solution myself:
The browser sends the if-no-match
header itself when it sees url had the e-tag
header on a previous request
. Browser saves the e-tag
with 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-get
to check the e-tag
:
Set the max-age header to the lowest
(for me 60s works great)cache
expires, thebrowser will send a conditional-get
to check if the expired cached resource
is valid. If the if-no-match
header matches with e-tag
. The server sends the response back with 304: Not-Modified
header. This means the expired cached resource is valid and can be used.Upvotes: 0