Skip to content

Commit 024040c

Browse files
committed
Make / work under FastBoot
This commit adds a transparent cache called "fetcher" to prevent making 2 XHRs (both from the frontend to the backend).
1 parent 8f56f5f commit 024040c

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

Diff for: app/controllers/index.js

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
11
import Controller from '@ember/controller';
2-
import { computed } from '@ember/object';
3-
import { readOnly } from '@ember/object/computed';
4-
5-
import ajax from 'ember-fetch/ajax';
6-
import { task } from 'ember-concurrency';
72

83
export default Controller.extend({
9-
model: readOnly('dataTask.lastSuccessful.value'),
10-
11-
hasData: computed('dataTask.{lastSuccessful,isRunning}', function() {
12-
return this.get('dataTask.lastSuccessful') || !this.get('dataTask.isRunning');
13-
}),
14-
15-
dataTask: task(function*() {
16-
let data = yield ajax('/api/v1/summary');
17-
18-
addCrates(this.store, data.new_crates);
19-
addCrates(this.store, data.most_downloaded);
20-
addCrates(this.store, data.just_updated);
21-
addCrates(this.store, data.most_recently_downloaded);
22-
23-
return data;
24-
}).drop(),
4+
hasData: true,
255
});
26-
27-
function addCrates(store, crates) {
28-
for (let i = 0; i < crates.length; i++) {
29-
crates[i] = store.push(store.normalize('crate', crates[i]));
30-
}
31-
}

Diff for: app/routes/index.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import Route from '@ember/routing/route';
2+
import { inject as service } from '@ember/service';
23

34
export default Route.extend({
5+
fetcher: service(),
6+
47
headTags() {
58
return [
69
{
@@ -13,8 +16,21 @@ export default Route.extend({
1316
];
1417
},
1518

16-
setupController(controller) {
17-
this.controllerFor('application').set('searchQuery', null);
18-
controller.dataTask.perform();
19+
model() {
20+
return this.fetcher.ajax('/api/v1/summary');
21+
},
22+
23+
// eslint-disable-next-line no-unused-vars
24+
afterModel(model, transition) {
25+
addCrates(this.store, model.new_crates);
26+
addCrates(this.store, model.most_downloaded);
27+
addCrates(this.store, model.just_updated);
28+
addCrates(this.store, model.most_recently_downloaded);
1929
},
2030
});
31+
32+
function addCrates(store, crates) {
33+
for (let i = 0; i < crates.length; i++) {
34+
crates[i] = store.push(store.normalize('crate', crates[i]));
35+
}
36+
}

Diff for: app/services/fetcher.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Service, { inject as service } from '@ember/service';
2+
import ajax from 'ember-fetch/ajax';
3+
4+
export default Service.extend({
5+
fastboot: service(),
6+
7+
ajax(url) {
8+
let fastboot = this.fastboot;
9+
let shoebox = this.fastboot.shoebox;
10+
let cache = shoebox.retrieve('ajax-cache');
11+
if (!cache) {
12+
cache = {};
13+
}
14+
15+
if (cache[url]) {
16+
return cache[url];
17+
}
18+
19+
return ajax(url).then(function(resp) {
20+
if (shoebox && fastboot.isFastBoot) {
21+
cache[url] = deepCopy(resp);
22+
shoebox.put('ajax-cache', cache);
23+
}
24+
return resp;
25+
});
26+
},
27+
});
28+
29+
function deepCopy(obj) {
30+
return JSON.parse(JSON.stringify(obj));
31+
}

0 commit comments

Comments
 (0)