From 61db6540f6cb0b3356beb102e62e7149d301556a Mon Sep 17 00:00:00 2001 From: chris48s Date: Fri, 8 Jun 2018 21:29:17 +0100 Subject: [PATCH 01/15] tell browsers and downstream caches to cache for 10 mins --- lib/request-handler.js | 10 ++++------ lib/request-handler.spec.js | 5 +++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index ae810741d4e6f..596b4e5a68981 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -79,14 +79,12 @@ function handleRequest (makeBadge, handlerOptions) { return (queryParams, match, end, ask) => { const reqTime = new Date(); + let maxAge = 600; if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { - ask.res.setHeader('Cache-Control', 'max-age=' + queryParams.maxAge); - ask.res.setHeader('Expires', new Date(+reqTime + queryParams.maxAge * 1000).toGMTString()); - } else { - // Cache management - no cache, so it won't be cached by GitHub's CDN. - ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); - ask.res.setHeader('Expires', reqTime.toGMTString()); // Proxies, GitHub, see #221. + maxAge = queryParams.maxAge; } + ask.res.setHeader('Cache-Control', 'max-age=' + maxAge); + ask.res.setHeader('Expires', new Date(+reqTime + maxAge * 1000).toGMTString()); ask.res.setHeader('Date', reqTime.toGMTString()); diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 665c342547816..cafae8eab3140 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -105,9 +105,10 @@ 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 + 10 mins', async function () { 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'))) + 600000).toGMTString(); + expect(res.headers.get('expires')).to.equal(expectedExpiry); }); it('should set the expires header to current time + max-age', async function () { From 6b54bf0e283f4b1af504e25972c9a6c1cf85e29a Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 10 Jun 2018 12:02:43 +0100 Subject: [PATCH 02/15] set Cache-Control: no-cache, no-store, must-revalidate if maxAge=0 --- lib/request-handler.js | 9 +++++++-- lib/request-handler.spec.js | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index 596b4e5a68981..7c81acb07895a 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -83,8 +83,13 @@ function handleRequest (makeBadge, handlerOptions) { if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { maxAge = queryParams.maxAge; } - ask.res.setHeader('Cache-Control', 'max-age=' + maxAge); - ask.res.setHeader('Expires', new Date(+reqTime + maxAge * 1000).toGMTString()); + if (maxAge === 0) { + ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); + ask.res.setHeader('Expires', reqTime.toGMTString()); + } else { + ask.res.setHeader('Cache-Control', 'max-age=' + maxAge); + ask.res.setHeader('Expires', new Date(+reqTime + maxAge * 1000).toGMTString()); + } ask.res.setHeader('Date', reqTime.toGMTString()); diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index cafae8eab3140..4b1b867afce0e 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -109,12 +109,20 @@ describe('The request handler', function() { const res = await fetch(`${baseUri}/testing/123.json`); 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 the expires header to current time + max-age', async function () { 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 set Cache-Control: no-cache, no-store, must-revalidate if maxAge=0', async function () { + const res = await fetch(`${baseUri}/testing/123.json?maxAge=0`); + 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 () { From 806cac9771bb0d694bae91434052143edddcf026 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 10 Jun 2018 12:04:42 +0100 Subject: [PATCH 03/15] change the default to 15 mins --- lib/request-handler.js | 2 +- lib/request-handler.spec.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index 7c81acb07895a..95ca3f1f8caca 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -79,7 +79,7 @@ function handleRequest (makeBadge, handlerOptions) { return (queryParams, match, end, ask) => { const reqTime = new Date(); - let maxAge = 600; + let maxAge = 900; if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { maxAge = queryParams.maxAge; } diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 4b1b867afce0e..8a22e5f3c236e 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -105,11 +105,11 @@ describe('The request handler', function() { expect(handlerCallCount).to.equal(1); }); - it('should set the expires header to current time + 10 mins', async function () { + it('should set the expires header to current time + 15 mins', async function () { const res = await fetch(`${baseUri}/testing/123.json`); - const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 600000).toGMTString(); + 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=600'); + expect(res.headers.get('cache-control')).to.equal('max-age=900'); }); it('should set the expires header to current time + max-age', async function () { From af74b9027eaa632ce3d89f866bd0fffa7ba3231b Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 10 Jun 2018 12:50:18 +0100 Subject: [PATCH 04/15] set expiry headers based on LONG_CACHE setting --- lib/request-handler.js | 5 ++++- lib/request-handler.spec.js | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index 95ca3f1f8caca..b908c6fbff3b0 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -1,5 +1,6 @@ 'use strict'; +const envFlag = require('node-env-flag'); // eslint-disable-next-line node/no-deprecated-api const domain = require('domain'); const request = require('request'); @@ -79,8 +80,10 @@ function handleRequest (makeBadge, handlerOptions) { return (queryParams, match, end, ask) => { const reqTime = new Date(); - let maxAge = 900; + let maxAge = envFlag(process.env.LONG_CACHE, false) ? 900 : 0; if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { + // always let queryParams.maxAge override the default + // regardless of env.LONG_CACHE setting maxAge = queryParams.maxAge; } if (maxAge === 0) { diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 8a22e5f3c236e..2d820939f8233 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -33,9 +33,11 @@ describe('The request handler', function() { before(analytics.load); let camp; + let initialLongCache; beforeEach(function (done) { camp = Camp.start({ port: config.port, hostname: '::' }); camp.on('listening', () => done()); + initialLongCache = process.env.LONG_CACHE; }); afterEach(function (done) { clearRequestCache(); @@ -43,6 +45,7 @@ describe('The request handler', function() { camp.close(() => done()); camp = null; } + process.env.LONG_CACHE = initialLongCache; }); describe('the options object calling style', function() { @@ -105,7 +108,8 @@ describe('The request handler', function() { expect(handlerCallCount).to.equal(1); }); - it('should set the expires header to current time + 15 mins', async function () { + it('should set the expires header to current time + 15 mins if LONG_CACHE on', async function () { + process.env.LONG_CACHE = 1; const res = await fetch(`${baseUri}/testing/123.json`); const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 900000).toGMTString(); expect(res.headers.get('expires')).to.equal(expectedExpiry); @@ -125,6 +129,13 @@ describe('The request handler', function() { 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 LONG_CACHE off', async function () { + process.env.LONG_CACHE = 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'); + }); + describe('the cache key', function () { const expectedCacheKey = '/testing/123.json?colorB=123&label=foo'; it('should match expected and use canonical order - 1', async function () { From a9d06bd797ebc83df61b1572dcb64e167eae893a Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 11 Jun 2018 20:07:17 +0100 Subject: [PATCH 05/15] only set initial value of LONG_CACHE once --- lib/request-handler.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 2d820939f8233..97cf5a09e65b4 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -33,11 +33,12 @@ describe('The request handler', function() { before(analytics.load); let camp; - let initialLongCache; + const initialLongCache = process.env.LONG_CACHE; + beforeEach(function (done) { camp = Camp.start({ port: config.port, hostname: '::' }); camp.on('listening', () => done()); - initialLongCache = process.env.LONG_CACHE; + process.env.LONG_CACHE = initialLongCache; }); afterEach(function (done) { clearRequestCache(); @@ -45,7 +46,6 @@ describe('The request handler', function() { camp.close(() => done()); camp = null; } - process.env.LONG_CACHE = initialLongCache; }); describe('the options object calling style', function() { From d6f6d2e91323e66a2ca6bad00519b534b735be0d Mon Sep 17 00:00:00 2001 From: chris48s Date: Sat, 16 Jun 2018 14:36:01 +0100 Subject: [PATCH 06/15] if service category is 'debug', exclude from examples This gives us a category for badges like 'flip' or 'servertime' for debug purposes which is excluded from the examples. --- lib/all-badge-examples.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 7664e6b38a517..49fea3f8934e5 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -2219,6 +2219,9 @@ function findCategory(wantedCategory) { function loadExamples() { loadServiceClasses().forEach(ServiceClass => { + if (ServiceClass.category === 'debug') { + return; + } const category = findCategory(ServiceClass.category); if (category === undefined) { throw Error(`Unknown category ${ServiceClass.category} referenced in ${ServiceClass.name}`); From 70d9e0d9d12577b987038fd74b61f41bea27f92e Mon Sep 17 00:00:00 2001 From: chris48s Date: Sat, 16 Jun 2018 14:38:06 +0100 Subject: [PATCH 07/15] add servertime badge to help with cache header debugging --- services/time/time.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 services/time/time.js diff --git a/services/time/time.js b/services/time/time.js new file mode 100644 index 0000000000000..84453520b7e52 --- /dev/null +++ b/services/time/time.js @@ -0,0 +1,34 @@ +'use strict'; + +const BaseService = require('../base'); + +module.exports = class Time extends BaseService { + + async handle({library}) { + return { message: new Date() }; + } + + // Metadata + static get defaultBadgeData() { + return { + label: 'time', + color: 'blue', + }; + } + + static get category() { + return 'debug'; + } + + static get url() { + return { + base: 'servertime', + format: '', + capture: [] + }; + } + + static get examples() { + return []; + } +}; From f5e69afabbed2655781005d70e59449439012a34 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 15 Jul 2018 21:07:46 +0100 Subject: [PATCH 08/15] set max-age header with BADGE_MAX_AGE_SECONDS, default 0 - avoid overloading LONG_CACHE - move confguration of cache length to deploy settings instead of source code - default to no cache for dev mode --- lib/request-handler.js | 5 ++--- lib/request-handler.spec.js | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index b908c6fbff3b0..1f65b7ac0a9a5 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -1,6 +1,5 @@ 'use strict'; -const envFlag = require('node-env-flag'); // eslint-disable-next-line node/no-deprecated-api const domain = require('domain'); const request = require('request'); @@ -80,10 +79,10 @@ function handleRequest (makeBadge, handlerOptions) { return (queryParams, match, end, ask) => { const reqTime = new Date(); - let maxAge = envFlag(process.env.LONG_CACHE, false) ? 900 : 0; + let maxAge = parseInt(process.env.BADGE_MAX_AGE_SECONDS) || 0; if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { // always let queryParams.maxAge override the default - // regardless of env.LONG_CACHE setting + // regardless of env.BADGE_MAX_AGE_SECONDS setting maxAge = queryParams.maxAge; } if (maxAge === 0) { diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 97cf5a09e65b4..49b01cf71905d 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -33,12 +33,12 @@ describe('The request handler', function() { before(analytics.load); let camp; - const initialLongCache = process.env.LONG_CACHE; + const initialBadgeMaxAge = process.env.BADGE_MAX_AGE_SECONDS; beforeEach(function (done) { camp = Camp.start({ port: config.port, hostname: '::' }); camp.on('listening', () => done()); - process.env.LONG_CACHE = initialLongCache; + process.env.BADGE_MAX_AGE_SECONDS = initialBadgeMaxAge; }); afterEach(function (done) { clearRequestCache(); @@ -108,8 +108,8 @@ describe('The request handler', function() { expect(handlerCallCount).to.equal(1); }); - it('should set the expires header to current time + 15 mins if LONG_CACHE on', async function () { - process.env.LONG_CACHE = 1; + 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`); const expectedExpiry = new Date(+(new Date(res.headers.get('date'))) + 900000).toGMTString(); expect(res.headers.get('expires')).to.equal(expectedExpiry); @@ -129,8 +129,8 @@ describe('The request handler', function() { 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 LONG_CACHE off', async function () { - process.env.LONG_CACHE = 0; + 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'); From 5619ded3a2683c8e62ff48a4afc8c2689f83b5b9 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 15 Jul 2018 21:17:45 +0100 Subject: [PATCH 09/15] ensure maxAge is always an int --- lib/request-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/request-handler.js b/lib/request-handler.js index 1f65b7ac0a9a5..51c062e5684f0 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -83,7 +83,7 @@ function handleRequest (makeBadge, handlerOptions) { if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { // always let queryParams.maxAge override the default // regardless of env.BADGE_MAX_AGE_SECONDS setting - maxAge = queryParams.maxAge; + maxAge = parseInt(queryParams.maxAge); } if (maxAge === 0) { ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); From 30232ff4911c584b7831e3391f94634fa3d23ac1 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 15 Jul 2018 21:22:18 +0100 Subject: [PATCH 10/15] reset env in afterEach() --- lib/request-handler.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 49b01cf71905d..1d48b87d04bcc 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -38,7 +38,6 @@ describe('The request handler', function() { beforeEach(function (done) { camp = Camp.start({ port: config.port, hostname: '::' }); camp.on('listening', () => done()); - process.env.BADGE_MAX_AGE_SECONDS = initialBadgeMaxAge; }); afterEach(function (done) { clearRequestCache(); @@ -46,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() { From 0b1f846079a4b23e9caa0e3b63d072fa1e4c6519 Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 15 Jul 2018 21:32:09 +0100 Subject: [PATCH 11/15] remove pointless param --- services/time/time.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/time/time.js b/services/time/time.js index 84453520b7e52..f2114182b7433 100644 --- a/services/time/time.js +++ b/services/time/time.js @@ -4,7 +4,7 @@ const BaseService = require('../base'); module.exports = class Time extends BaseService { - async handle({library}) { + async handle() { return { message: new Date() }; } From ee69f7af115bdd3a8321983ad40ac18fb985dc2d Mon Sep 17 00:00:00 2001 From: chris48s Date: Sun, 15 Jul 2018 21:36:08 +0100 Subject: [PATCH 12/15] explain why we send Cache-Control and Expires in a comment --- lib/request-handler.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/request-handler.js b/lib/request-handler.js index 51c062e5684f0..3ba3df9aac62b 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -85,6 +85,8 @@ function handleRequest (makeBadge, handlerOptions) { // regardless of env.BADGE_MAX_AGE_SECONDS setting maxAge = parseInt(queryParams.maxAge); } + // send both Cache-Control max-age and Expires + // in case the client implements HTTP/1.0 but not HTTP/1.1 if (maxAge === 0) { ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); ask.res.setHeader('Expires', reqTime.toGMTString()); From 4864d21dceec7697da7faa59d250e8965db6ef75 Mon Sep 17 00:00:00 2001 From: chris48s Date: Tue, 17 Jul 2018 20:10:08 +0100 Subject: [PATCH 13/15] move and clarify debug services check --- lib/all-badge-examples.js | 7 ++++--- services/time/time.js | 3 --- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/all-badge-examples.js b/lib/all-badge-examples.js index 49fea3f8934e5..6caeb4c40f262 100644 --- a/lib/all-badge-examples.js +++ b/lib/all-badge-examples.js @@ -2219,11 +2219,12 @@ function findCategory(wantedCategory) { function loadExamples() { loadServiceClasses().forEach(ServiceClass => { - if (ServiceClass.category === 'debug') { - return; - } const category = findCategory(ServiceClass.category); if (category === undefined) { + if (ServiceClass.category === 'debug') { + // we don't want to show debug services on the examples page + return; + } throw Error(`Unknown category ${ServiceClass.category} referenced in ${ServiceClass.name}`); } const prepared = ServiceClass.prepareExamples(); diff --git a/services/time/time.js b/services/time/time.js index f2114182b7433..8ea7ca23f1ec9 100644 --- a/services/time/time.js +++ b/services/time/time.js @@ -28,7 +28,4 @@ module.exports = class Time extends BaseService { }; } - static get examples() { - return []; - } }; From 44ee9307192ed2c1ff4d3a6082f670012794ddc1 Mon Sep 17 00:00:00 2001 From: chris48s Date: Wed, 18 Jul 2018 19:27:40 +0100 Subject: [PATCH 14/15] ignore maxAge GET param if less than env.BADGE_MAX_AGE_SECONDS --- frontend/components/usage.js | 4 +++- lib/request-handler.js | 10 +++++++--- lib/request-handler.spec.js | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/frontend/components/usage.js b/frontend/components/usage.js index c4004b14f4df2..a804d14b861bf 100644 --- a/frontend/components/usage.js +++ b/frontend/components/usage.js @@ -5,6 +5,8 @@ import DynamicBadgeMaker from './dynamic-badge-maker'; import { staticBadgeUrl } from '../lib/badge-url'; import { advertisedStyles } from '../../lib/supported-features'; +const maxAge = parseInt(process.env.BADGE_MAX_AGE_SECONDS) || 0; + export default class Usage extends React.PureComponent { static propTypes = { baseUri: PropTypes.string.isRequired, @@ -202,7 +204,7 @@ export default class Usage extends React.PureComponent { ?maxAge=3600 - Set the HTTP cache lifetime in secs + Set the HTTP cache lifetime in secs (values below {maxAge} will be ignored) diff --git a/lib/request-handler.js b/lib/request-handler.js index 3ba3df9aac62b..d4bc0964e0415 100644 --- a/lib/request-handler.js +++ b/lib/request-handler.js @@ -80,9 +80,13 @@ function handleRequest (makeBadge, handlerOptions) { const reqTime = new Date(); let maxAge = parseInt(process.env.BADGE_MAX_AGE_SECONDS) || 0; - if (queryParams.maxAge !== undefined && /^[0-9]+$/.test(queryParams.maxAge)) { - // always let queryParams.maxAge override the default - // regardless of env.BADGE_MAX_AGE_SECONDS setting + if ( + queryParams.maxAge !== undefined + && /^[0-9]+$/.test(queryParams.maxAge) + && parseInt(queryParams.maxAge) > maxAge + ) { + // only queryParams.maxAge to override the default + // if it is greater than env.BADGE_MAX_AGE_SECONDS maxAge = parseInt(queryParams.maxAge); } // send both Cache-Control max-age and Expires diff --git a/lib/request-handler.spec.js b/lib/request-handler.spec.js index 1d48b87d04bcc..68f0b3d331d28 100644 --- a/lib/request-handler.spec.js +++ b/lib/request-handler.spec.js @@ -116,15 +116,25 @@ describe('The request handler', function() { 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 () { - const res = await fetch(`${baseUri}/testing/123.json?maxAge=0`); + 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'); }); From bb067086b607c67ac7b176365244ae5040782296 Mon Sep 17 00:00:00 2001 From: chris48s Date: Thu, 19 Jul 2018 21:38:04 +0100 Subject: [PATCH 15/15] don't specify default in the docs --- frontend/components/usage.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frontend/components/usage.js b/frontend/components/usage.js index a804d14b861bf..52ecf7f02429c 100644 --- a/frontend/components/usage.js +++ b/frontend/components/usage.js @@ -5,8 +5,6 @@ import DynamicBadgeMaker from './dynamic-badge-maker'; import { staticBadgeUrl } from '../lib/badge-url'; import { advertisedStyles } from '../../lib/supported-features'; -const maxAge = parseInt(process.env.BADGE_MAX_AGE_SECONDS) || 0; - export default class Usage extends React.PureComponent { static propTypes = { baseUri: PropTypes.string.isRequired, @@ -204,7 +202,7 @@ export default class Usage extends React.PureComponent { ?maxAge=3600 - Set the HTTP cache lifetime in secs (values below {maxAge} will be ignored) + Set the HTTP cache lifetime in secs (values below the default will be ignored)