send longer cache headers by default (#1725)

* tell browsers and downstream caches to cache for
  `env.BADGE_MAX_AGE_SECONDS`, default 0 for dev

* set Cache-Control: no-cache, no-store, must-revalidate if maxAge=0

* add servertime badge to help with cache header debugging

* if service category is 'debug', exclude from examples

* ignore maxAge GET param if less than `env.BADGE_MAX_AGE_SECONDS`
This commit is contained in:
chris48s
2018-07-20 20:24:02 +01:00
committed by GitHub
parent fd76819c62
commit b9db222c8a
5 changed files with 86 additions and 10 deletions

View File

@@ -33,6 +33,8 @@ describe('The request handler', function() {
before(analytics.load);
let camp;
const initialBadgeMaxAge = process.env.BADGE_MAX_AGE_SECONDS;
beforeEach(function (done) {
camp = Camp.start({ port: config.port, hostname: '::' });
camp.on('listening', () => done());
@@ -43,6 +45,7 @@ describe('The request handler', function() {
camp.close(() => done());
camp = null;
}
process.env.BADGE_MAX_AGE_SECONDS = initialBadgeMaxAge;
});
describe('the options object calling style', function() {
@@ -105,15 +108,42 @@ describe('The request handler', function() {
expect(handlerCallCount).to.equal(1);
});
it('should set the expires header to current time', async function () {
it('should set the expires header to current time + BADGE_MAX_AGE_SECONDS', async function () {
process.env.BADGE_MAX_AGE_SECONDS = 900;
const res = await fetch(`${baseUri}/testing/123.json`);
expect(res.headers.get('expires')).to.equal(res.headers.get('date'));
const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 900000).toGMTString();
expect(res.headers.get('expires')).to.equal(expectedExpiry);
expect(res.headers.get('cache-control')).to.equal('max-age=900');
});
it('should set the expires header to current time + max-age', async function () {
it('should set the expires header to current time + maxAge', async function () {
process.env.BADGE_MAX_AGE_SECONDS = 0;
const res = await fetch(`${baseUri}/testing/123.json?maxAge=3600`);
const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 3600000).toGMTString();
expect(res.headers.get('expires')).to.equal(expectedExpiry);
expect(res.headers.get('cache-control')).to.equal('max-age=3600');
});
it('should ignore maxAge if maxAge < BADGE_MAX_AGE_SECONDS', async function () {
process.env.BADGE_MAX_AGE_SECONDS = 600;
const res = await fetch(`${baseUri}/testing/123.json?maxAge=300`);
const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 600000).toGMTString();
expect(res.headers.get('expires')).to.equal(expectedExpiry);
expect(res.headers.get('cache-control')).to.equal('max-age=600');
});
it('should set Cache-Control: no-cache, no-store, must-revalidate if maxAge=0', async function () {
process.env.BADGE_MAX_AGE_SECONDS = 0;
const res = await fetch(`${baseUri}/testing/123.json`);
expect(res.headers.get('expires')).to.equal(res.headers.get('date'));
expect(res.headers.get('cache-control')).to.equal('no-cache, no-store, must-revalidate');
});
it('should set Cache-Control: no-cache, no-store, must-revalidate if BADGE_MAX_AGE_SECONDS not set', async function () {
delete process.env.BADGE_MAX_AGE_SECONDS;
const res = await fetch(`${baseUri}/testing/123.json`);
expect(res.headers.get('expires')).to.equal(res.headers.get('date'));
expect(res.headers.get('cache-control')).to.equal('no-cache, no-store, must-revalidate');
});
describe('the cache key', function () {