diff --git a/ghost/mw-cache-control/lib/mw-cache-control.js b/ghost/mw-cache-control/lib/mw-cache-control.js index f89a0c56f2..6c9129469f 100644 --- a/ghost/mw-cache-control/lib/mw-cache-control.js +++ b/ghost/mw-cache-control/lib/mw-cache-control.js @@ -12,10 +12,18 @@ const isString = require('lodash/isString'); * @param {'public'|'private'} profile Use "private" if you do not want caching * @param {object} [options] * @param {number} [options.maxAge] The max-age in seconds to use when profile is "public" + * @param {number} [options.staleWhileRevalidate] The stale-while-revalidate in seconds to use when profile is "public" */ const cacheControl = (profile, options = {maxAge: 0}) => { + const isOptionHasProperty = property => Object.prototype.hasOwnProperty.call(options, property); + const publicOptions = [ + 'public', + `max-age=${options.maxAge}`, + isOptionHasProperty('staleWhileRevalidate') ? `stale-while-revalidate=${options.staleWhileRevalidate}` : '' + ]; + const profiles = { - public: `public, max-age=${options.maxAge}`, + public: publicOptions.filter(option => option).join(', '), private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0' }; diff --git a/ghost/mw-cache-control/test/cache-control.test.js b/ghost/mw-cache-control/test/cache-control.test.js index bafc680f31..e7e1cd9275 100644 --- a/ghost/mw-cache-control/test/cache-control.test.js +++ b/ghost/mw-cache-control/test/cache-control.test.js @@ -34,6 +34,15 @@ describe('Cache-Control middleware', function () { }); }); + it('correctly sets the public profile headers with staleWhileRevalidate', function (done) { + cacheControl('public', {maxAge: 1, staleWhileRevalidate: 9})(null, res, function (a) { + should.not.exist(a); + res.set.calledOnce.should.be.true(); + res.set.calledWith({'Cache-Control': 'public, max-age=1, stale-while-revalidate=9'}).should.be.true(); + done(); + }); + }); + it('correctly sets the private profile headers', function (done) { cacheControl('private')(null, res, function (a) { should.not.exist(a);