Skip to content

Commit a34f6c4

Browse files
committed
Auto merge of #1937 - kzys:fastboot-index, r=carols10cents
Make / work under FastBoot This PR adds a transparent cache called "fetcher" to prevent making 2 XHRs (both from the frontend to the backend).
2 parents b308ac8 + 9b4aaf5 commit a34f6c4

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
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

+23-2
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,26 @@ export default Route.extend({
1316
];
1417
},
1518

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

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)