Skip to content

Commit

Permalink
Merge pull request #3044 from csabapalfi/disable-cache-control
Browse files Browse the repository at this point in the history
Enable disabling cache-control headers
  • Loading branch information
hueniverse committed Mar 11, 2016
2 parents 4869dd1 + edbef27 commit 687a378
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 3 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,9 @@ following options:
Ignored if the method is an arrow function.

- `cache` - if the route method is 'GET', the route can be configured to include caching
directives in the response using the following options:
directives in the response.
The default `Cache-Control: no-cache` header can be disabled by setting `cache` to `false`.
Caching can be customized using an object with the following options:
- `privacy` - determines the privacy flag included in client-side caching using the
'Cache-Control' header. Values are:
- `'default'` - no privacy flag. This is the default setting.
Expand Down
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internals.routeBase = Joi.object({
expiresAt: Joi.string(),
privacy: Joi.string().valid('default', 'public', 'private'),
statuses: Joi.array().items(Joi.number().integer().min(200)).min(1)
}),
}).allow(false),
cors: Joi.object({
origin: Joi.array().min(1),
maxAge: Joi.number(),
Expand Down
5 changes: 3 additions & 2 deletions lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,12 @@ internals.Empty.prototype._read = function (/* size */) {

internals.cache = function (response) {

if (response.headers['cache-control']) {
const request = response.request;

if (response.headers['cache-control'] || request.route.settings.cache === false ) {
return;
}

const request = response.request;
const policy = request._route._cache && (request.route.settings.cache._statuses[response.statusCode] || (response.statusCode === 304 && request.route.settings.cache._statuses['200']));
if (policy ||
response.settings.ttl) {
Expand Down
32 changes: 31 additions & 1 deletion test/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ describe('transmission', () => {
});
});

it('sets caching headers', (done) => {
it('sets specific caching headers', (done) => {

const server = new Hapi.Server();
server.register(Inert, Hoek.ignore);
Expand All @@ -580,6 +580,36 @@ describe('transmission', () => {
done();
});
});

it('sets caching headers', (done) => {

const server = new Hapi.Server();
server.register(Inert, Hoek.ignore);
server.connection();
server.route({ method: 'GET', path: '/public/{path*}', handler: { directory: { path: __dirname, listing: false, index: false } } });

server.inject('/public/transmit.js', (res) => {

expect(res.statusCode).to.equal(200);
expect(res.headers['cache-control']).to.equal('no-cache');
done();
});
});

it('does not set caching headers if disabled', (done) => {

const server = new Hapi.Server();
server.register(Inert, Hoek.ignore);
server.connection();
server.route({ method: 'GET', path: '/public/{path*}', config: { cache: false }, handler: { directory: { path: __dirname, listing: false, index: false } } });

server.inject('/public/transmit.js', (res) => {

expect(res.statusCode).to.equal(200);
expect(res.headers['cache-control']).to.be.undefined();
done();
});
});
});

describe('transmit()', () => {
Expand Down

0 comments on commit 687a378

Please sign in to comment.