Skip to content

Commit

Permalink
Make / work under FastBoot
Browse files Browse the repository at this point in the history
This commit adds a transparent cache called "fetcher" to prevent making 2 XHRs
(both from the frontend to the backend).
  • Loading branch information
kzys committed Dec 5, 2019
1 parent 8f56f5f commit fb0fca0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
28 changes: 1 addition & 27 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { readOnly } from '@ember/object/computed';

import ajax from 'ember-fetch/ajax';
import { task } from 'ember-concurrency';

export default Controller.extend({
model: readOnly('dataTask.lastSuccessful.value'),

hasData: computed('dataTask.{lastSuccessful,isRunning}', function() {
return this.get('dataTask.lastSuccessful') || !this.get('dataTask.isRunning');
}),

dataTask: task(function*() {
let data = yield ajax('/api/v1/summary');

addCrates(this.store, data.new_crates);
addCrates(this.store, data.most_downloaded);
addCrates(this.store, data.just_updated);
addCrates(this.store, data.most_recently_downloaded);

return data;
}).drop(),
hasData: true,
});

function addCrates(store, crates) {
for (let i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
}
}
21 changes: 18 additions & 3 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
fetcher: service(),

headTags() {
return [
{
Expand All @@ -13,8 +16,20 @@ export default Route.extend({
];
},

setupController(controller) {
this.controllerFor('application').set('searchQuery', null);
controller.dataTask.perform();
model() {
return this.fetcher.ajax('/api/v1/summary');
},

afterModel(model, transition) {
addCrates(this.store, model.new_crates);
addCrates(this.store, model.most_downloaded);
addCrates(this.store, model.just_updated);
addCrates(this.store, model.most_recently_downloaded);
},
});

function addCrates(store, crates) {
for (let i = 0; i < crates.length; i++) {
crates[i] = store.push(store.normalize('crate', crates[i]));
}
}
31 changes: 31 additions & 0 deletions app/services/fetcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Service, { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Service.extend({
fastboot: service(),

ajax(url) {
let fastboot = this.fastboot;
let shoebox = this.fastboot.shoebox;
let cache = shoebox.retrieve('ajax-cache');
if (!cache) {
cache = {};
}

if (cache[url]) {
return cache[url];
}

return ajax(url).then(function(resp) {
if (shoebox && fastboot.isFastBoot) {
cache[url] = deepCopy(resp);
shoebox.put('ajax-cache', cache);
}
return resp;
});
},
});

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

0 comments on commit fb0fca0

Please sign in to comment.