Skip to content

Commit

Permalink
no cache override. Closes #3206
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 5, 2016
1 parent 4311884 commit 9d8d617
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 89 deletions.
3 changes: 2 additions & 1 deletion lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ exports.connection = {
},
routes: {
cache: {
statuses: [200, 204] // Array of HTTP status codes for which cache-control header is set
statuses: [200, 204], // Array of HTTP status codes for which cache-control header is set
otherwise: 'no-cache'
},
cors: false, // CORS headers
files: {
Expand Down
1 change: 1 addition & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ exports = module.exports = internals.Route = function (route, connection, plugin
// Cache

if (this.method === 'get' &&
typeof this.settings.cache === 'object' &&
(this.settings.cache.expiresIn || this.settings.cache.expiresAt)) {

this.settings.cache._statuses = Hoek.mapToObject(this.settings.cache.statuses);
Expand Down
6 changes: 4 additions & 2 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ internals.routeBase = Joi.object({
expiresIn: Joi.number(),
expiresAt: Joi.string(),
privacy: Joi.string().valid('default', 'public', 'private'),
statuses: Joi.array().items(Joi.number().integer().min(200)).min(1)
}).allow(false),
statuses: Joi.array().items(Joi.number().integer().min(200)).min(1).single(),
otherwise: Joi.string().required()
})
.allow(false),
cors: Joi.object({
origin: Joi.array().min(1),
maxAge: Joi.number(),
Expand Down
13 changes: 7 additions & 6 deletions lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,22 +387,23 @@ internals.cache = function (response) {

const request = response.request;

if (response.headers['cache-control'] ||
!request.route.settings.cache) {

if (response.headers['cache-control']) {
return;
}

const policy = request._route._cache && (request.route.settings.cache._statuses[response.statusCode] || (response.statusCode === 304 && request.route.settings.cache._statuses['200']));
const policy = request.route.settings.cache &&
request._route._cache &&
(request.route.settings.cache._statuses[response.statusCode] || (response.statusCode === 304 && request.route.settings.cache._statuses['200']));

if (policy ||
response.settings.ttl) {

const ttl = (response.settings.ttl !== null ? response.settings.ttl : request._route._cache.ttl());
const privacy = (request.auth.isAuthenticated || response.headers['set-cookie'] ? 'private' : request.route.settings.cache.privacy || 'default');
response._header('cache-control', 'max-age=' + Math.floor(ttl / 1000) + ', must-revalidate' + (privacy !== 'default' ? ', ' + privacy : ''));
}
else {
response._header('cache-control', 'no-cache');
else if (request.route.settings.cache) {
response._header('cache-control', request.route.settings.cache.otherwise);
}
};

Expand Down
Loading

0 comments on commit 9d8d617

Please sign in to comment.