Skip to content

Commit f4e1ce6

Browse files
committed
Don't make duplicated requests through Ember Data
This will prevent duplicated requests on `/crates` and other Ember Data-powered pages.
1 parent d46b4e3 commit f4e1ce6

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

app/adapters/application.js

+15
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@ import { computed } from '@ember/object';
44

55
export default RESTAdapter.extend({
66
fastboot: service(),
7+
fetcher: service(),
78

89
namespace: 'api/v1',
910

11+
ajax: function(url, type, options) {
12+
if (type === 'GET') {
13+
let cache = this.fetcher.get(url);
14+
if (cache) {
15+
return cache;
16+
}
17+
}
18+
19+
return this._super(url, type, options).then(resp => {
20+
this.fetcher.put(url, resp);
21+
return resp;
22+
});
23+
},
24+
1025
headers: computed('fastboot.{isFastBoot,request.headers}', function() {
1126
if (this.fastboot.isFastBoot) {
1227
return { 'User-Agent': this.fastboot.request.headers.get('User-Agent') };

app/services/fetcher.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,36 @@ import ajax from 'ember-fetch/ajax';
44
export default Service.extend({
55
fastboot: service(),
66

7-
ajax(url) {
7+
get(url) {
8+
let shoebox = this.fastboot.shoebox;
9+
let cache = shoebox.retrieve('ajax-cache');
10+
if (!cache) {
11+
cache = {};
12+
}
13+
return cache[url];
14+
},
15+
16+
put(url, obj) {
817
let fastboot = this.fastboot;
918
let shoebox = this.fastboot.shoebox;
1019
let cache = shoebox.retrieve('ajax-cache');
1120
if (!cache) {
1221
cache = {};
1322
}
23+
if (shoebox && fastboot.isFastBoot) {
24+
cache[url] = deepCopy(obj);
25+
shoebox.put('ajax-cache', cache);
26+
}
27+
},
1428

15-
if (cache[url]) {
16-
return cache[url];
29+
ajax(url) {
30+
let resp = this.get(url);
31+
if (resp) {
32+
return resp;
1733
}
1834

19-
return ajax(url).then(function(resp) {
20-
if (shoebox && fastboot.isFastBoot) {
21-
cache[url] = deepCopy(resp);
22-
shoebox.put('ajax-cache', cache);
23-
}
35+
return ajax(url).then(resp => {
36+
this.put(url, resp);
2437
return resp;
2538
});
2639
},

0 commit comments

Comments
 (0)