Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Vue-resource can cancel the cache as Ajax do #190

Closed
ya00la opened this issue Dec 25, 2015 · 8 comments
Closed

Vue-resource can cancel the cache as Ajax do #190

ya00la opened this issue Dec 25, 2015 · 8 comments

Comments

@ya00la
Copy link

ya00la commented Dec 25, 2015

I use vue-resource for data requests, but found that the second request the same data source will appear the cache data, not the latest data, I would like to ask how to cancel the vue-resource cache settings

ajax cancle cache setting (cache:false)
$.ajax({
type: data_method,
url: data_url,
data:data_string,
dataType: 'json',
cache:false,
success: function(data){
console.log(data)
}
},

@ya00la ya00la changed the title Can vue-resource set up a cache like Ajax Vue-resource can cancel the cache as Ajax do Dec 25, 2015
@carlosingles
Copy link

I was having the same issue in IE, I fixed this on the server side by adding a Header on requests that were submitted by AJAX.

vue-resource already adds a X-Requested-With: XMLHttpRequest header, so all I had to do was check for this header on my server and then add a Expires: -1 Header on the response.

I am using Express, so this was fairly simple since there's already a req.xhr value that checks for the above header. I added the following middleware to the start of my app:

app.use(function(req, res, next){
  // check for AJAX request and turn off caching
  if(req.xhr) res.set('Expires', '-1')
  // continue
  next()
})

@libertyAlone
Copy link

you can set http request header Cache-Control

this.$http.get('/api', param, {
   headers: {
       'Cache-Control': 'no-cache'
   }
}).then(function (data) {});

@jakejscott
Copy link

I'm doing it like this but I need to make it safe

// http request/response interceptors
Vue.http.interceptors.push({
    request: function (request) {
        // todo(jake): do this properly, don't just blindly append ?..
        request.url += "?" + new Date().getTime();        
        return request;
    },
    response: function (response) {
        return response;
    }
});

@potasiyam
Copy link

@libertyAlone I tried with 'Cache-Control': 'no-cache' in my request header. But my browser is still caching the request. Temporarily solved it by appending timestamp with URL, but I need better solution. I also vote for a flag like jQuery ajax in vue-resource options.

@libern
Copy link

libern commented Jun 29, 2016

@superlogical Thanks for the solution

@jakejscott
Copy link

New solution

Vue.http.interceptors.push((request, next) => {
  request.url += (request.url.indexOf('?') > 0 ? '&' : '?') + `cb=${new Date().getTime()}`
  next()
})

@khalidazza
Copy link

khalidazza commented Apr 17, 2017

Hi folks,
I have the same issue and i tried all solutions listed before but the issue persist, i don't really know where the problem came from, i thought that chrome caches responses but the fuzzy situation is, why chrome keep cache from invited and private session?
PS: mozilla firefox works fine but i need the VueJs Devtools

@roboriaan
Copy link

Christ! Internet explorer must get on board or die

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants