Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add frontend cache query params #5775

Merged
merged 3 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ export default JSONAPIAdapter.extend(HasManyQueryAdapterMixin, FastbootAdapter,
return this._super(store, type, query);
},

ajaxOptions(url, type) {
const request = this._super(...arguments);

// The requests with public=true will not be authorized
if (type === 'GET') {
if (request.data.public) {delete request.headers[ENV['ember-simple-auth-token'].authorizationHeaderName]}

if (ENV.noCache === 'true') {
request.data.nocache = true;
}
}

return request;
},

/**
This method is called for every response that the adapter receives from the
API. If the response has a 401 status code it invalidates the session (see
Expand Down
2 changes: 1 addition & 1 deletion app/components/footer-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export default class FooterMain extends Component {
}

async didInsertElement() {
this.set('pages', sortBy(await this.cache.findAll('page'), 'index'));
this.set('pages', sortBy(await this.cache.query('pages', 'page', { public: true }), 'index'));
}
}
3 changes: 0 additions & 3 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ export default class ApplicationRoute extends Route.extend(ApplicationRouteMixin
}
}

const pages = this.cache.findAll('page');

return hash({
notifications,
pages,
cookiePolicy : this.settings.cookiePolicy,
cookiePolicyLink : this.settings.cookiePolicyLink,
socialLinks : this.settings
Expand Down
6 changes: 6 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,24 @@ export default class IndexRoute extends Route {
filteredEvents: this.store.query('event', {
upcoming : true,
include : 'event-topic,event-sub-topic,event-type,speakers-call',
cache : true,
public : true,
'page[size]' : 12
}),
featuredEvents: this.store.query('event', {
sort : 'starts-at',
include : 'event-topic,event-sub-topic,event-type,speakers-call',
filter : filterOptions,
cache : true,
public : true,
'page[size]' : 6
}),
callForSpeakersEvents: this.store.query('event', {
sort : 'starts-at',
include : 'event-topic,event-sub-topic,event-type,speakers-call',
filter : callForSpeakersFilter,
cache : true,
public : true,
'page[size]' : 6
})
});
Expand Down
11 changes: 9 additions & 2 deletions app/services/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,24 @@ export default class Cache extends Service.extend({
}

async findAll(model: string, options: any | null): Promise<any> {
const saved = await this.cacheData(model, () => this.store.findAll(model, options));
const saved = await this.cacheData(model, () => this.store.findAll(model, { cache: true, ...options }));
if (saved) {return saved;}
return this.store.peekAll(model);
}

async queryRecord(key: string, model: string, options: any | null): Promise<any> {
const saved = await this.cacheData(key, () => this.store.queryRecord(model, options));
const saved = await this.cacheData(key, () => this.store.queryRecord(model, { cache: true, ...options }));
if (saved) {return saved;}
return this.store.peekRecord(model, 1);
}

async query(key: string, model: string, options: any | null): Promise<any> {
options = { cache: true, ...options };
const saved = await this.cacheData(key, () => this.store.query(model, options));
if (saved) {return saved;}
return this.store.query(model, options);
}

clear(): void {
for (const key of Object.keys(localStorage)) {
if (key.startsWith(this.prefix)) {
Expand Down
2 changes: 1 addition & 1 deletion app/services/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default Service.extend({
* @private
*/
async _loadSettings() {
const settingsModel = await this.cache.queryRecord('settings', 'setting', {});
const settingsModel = await this.cache.queryRecord('settings', 'setting', { public: true });
this.store.modelFor('setting').eachAttribute(attributeName => {
this.set(attributeName, settingsModel.get(attributeName));
});
Expand Down
2 changes: 2 additions & 0 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ module.exports = function(environment) {
includeScheduler: true
},

noCache: process.env.NO_CACHE || 'false',

ifa: {
enabled : false,
inline : false
Expand Down