Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome is caching an HTTP PUT request

I have this bizarre issue with Chrome. It quite often appears to cache PUT requests.

The Details: I have an app using backbone.js and when trying to persist some changes to a model (backbone automatically generates a PUT request), Chrome just wont send that request to the server. It works perfectly fine in Firefox and IE (haven't seen the issue in Safari so far).

Here's a screenshot from the Chrome developer tools' Network tab. As you can see, the response for the PUT request is being returned from cache (the request doesn't hit the server!!) Chrome caches PUT request

Here's a screenshot of the header details of that same request. Once again, it's evident that Chrome doesn't bother sending the PUT request to the server. Chrome cached PUT request header

The payload of the request is JSON data. Any thoughts as to why this is happening / what I'm doing wrong?

UPDATE: Chromium has confirmed that this is indeed a bug on it's end (thanks Jan Hančič).

TEMPORARY SOLUTION I ended up overriding Backbone.sync method and appending a timestamp to the querystring of PUT, POST and DELETE requests so that they are always unique:

if(!options.data && model && (method == 'create' || method == 'update' || method == 'delete')) {
    params.url += (params.url.indexOf('?') == -1 ? '?' : '&') + '_=' + new Date().getTime();
}
like image 550
anushr Avatar asked Aug 01 '12 06:08

anushr


People also ask

IS PUT request cached?

A PUT request cannot be cached. Moreover, it invalidates cached data for request to the same URI done via HEAD or GET : PUT /pageX.

How do I stop chrome from caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Does browser cache POST request?

"Responses to POST method are not cacheable, UNLESS the response includes appropriate Cache-Control or Expires header fields." So, YES, you can cache POST request response but only if it arrives with appropriate headers. In most cases you don't want to cache the response.

What is caching in Chrome?

They make your online experience easier by saving browsing data. The cache remembers parts of pages, like images, to help them open faster during your next visit.


1 Answers

I use extra parameter to avoid caching:

url += '?_dc=' + Math.random().toFixed(20).replace('.', '');

I don't interpret this parameter on server side.

EDIT: Besides of chrome there are a lot things could cache requests - user's proxy server for instance. I think additional query parameter is a good solution to keep out of caching.

like image 148
Pavel Reznikov Avatar answered Oct 01 '22 18:10

Pavel Reznikov