Skip to content

Commit

Permalink
Handle query parameters correctly
Browse files Browse the repository at this point in the history
The options parameter is used to represent query parameters. So it must
be included to differentiate URLs with different query parameters.
  • Loading branch information
kzys committed Feb 20, 2020
1 parent f594ed8 commit a375466
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export default RESTAdapter.extend({

ajax: function(url, type, options) {
if (type === 'GET') {
let cache = this.fetcher.get(url);
let cache = this.fetcher.get(url, options);
if (cache) {
return cache;
}
}

return this._super(url, type, options).then(resp => {
this.fetcher.put(url, resp);
this.fetcher.put(url, options, resp);
return resp;
});
},
Expand Down
14 changes: 10 additions & 4 deletions app/services/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ const KEY = 'ajax-cache';
export default Service.extend({
fastboot: service(),

get(url) {
get(url, options) {
let shoebox = this.fastboot.shoebox;
if (!shoebox) {
return;
}
let cache = shoebox.retrieve(KEY) || {};
return cache[url];
let key = cacheKey(url, options);
return cache[key];
},

put(url, obj) {
put(url, options, obj) {
let fastboot = this.fastboot;
let shoebox = this.fastboot.shoebox;
if (!(shoebox && fastboot.isFastBoot)) {
return;
}

let cache = shoebox.retrieve(KEY) || {};
cache[url] = deepCopy(obj);
let key = cacheKey(url, options);
cache[key] = deepCopy(obj);
shoebox.put(KEY, cache);
},

Expand All @@ -40,6 +42,10 @@ export default Service.extend({
},
});

function cacheKey(url, options) {
return url + JSON.stringify(options);
}

function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}

0 comments on commit a375466

Please sign in to comment.