Skip to content

Commit

Permalink
Merge pull request #40 from firstandthird/stale-content
Browse files Browse the repository at this point in the history
Serve stale content
  • Loading branch information
ecwillis authored Oct 31, 2017
2 parents 6058aba + ae32e74 commit c9e31a2
Show file tree
Hide file tree
Showing 5 changed files with 1,423 additions and 293 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const aug = require('aug');
const renderHandler = require('./lib/handler.js');
const serverMethods = ['api', 'inject', 'yaml', 'method'];
const defaults = {
enableCache: false,
serveStale: false,
allowDebugQuery: true, //allows ?json=1
routeConfig: {},
debug: false,
Expand All @@ -18,17 +20,21 @@ exports.register = function(server, options, next) {
// todo: add caching options:
const methodOptions = {};
if (options.enableCache) {
const defaultOptions = {};
if (options.serveStale) {
defaultOptions.dropOnError = false;
}
switch (methodName) {
case 'api':
// cache key will be the url of the api call:
methodOptions.cache = Object.assign({}, options.cache);
methodOptions.cache = Object.assign({}, defaultOptions, options.cache);
methodOptions.generateKey = function(genRequest, url) {
return typeof url === 'string' ? url : url.url;
};
break;
case 'inject':
// cache key will be the path we're injecting to
methodOptions.cache = Object.assign({}, options.cache);
methodOptions.cache = Object.assign({}, defaultOptions, options.cache);
methodOptions.generateKey = function(genRequest, url) {
return url;
};
Expand Down
3 changes: 2 additions & 1 deletion methods/views/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = (request, api, allDone) => {
if (err) {
// boom response can be repackaged for hapi to pass back to the caller:
if (err.isBoom) {
return allDone(Boom.create(err.output.statusCode, err.data.payload.message));
const mssg = (err.data) ? err.data.payload.message : err.output.payload.messge;
return allDone(Boom.create(err.output.statusCode, mssg));
}
return allDone(err);
}
Expand Down
8 changes: 7 additions & 1 deletion methods/views/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ module.exports = (request, config, allDone) => {
if (methodData.type === 'inject') {
methodData.data = varson({ url: methodData.data }, request, varsonSettings).url;
}

let nocache = config.enableCache === false;
if (request.query.nocache === '1') {
nocache = true;
}

const methodName = nocache ? `${methodData.type}_noCache` : methodData.type;
request.server.methods.views[methodName](request, methodData.data, (err, result) => {
request.server.methods.views[methodName](request, methodData.data, (err, result, cacheData) => {
if (err && config.options.serveStale && cacheData && !nocache) {
request.server.log(['hapi-views', 'fetch'], {err, message: `${methodName} returned an error. Serving stale content`});
err = null;
}
out[methodData.type][methodData.key] = result;
return mapDone(err, out);
});
Expand Down
Loading

0 comments on commit c9e31a2

Please sign in to comment.