From 1c736f2159d83184d6815f7595f8f042f175053b Mon Sep 17 00:00:00 2001 From: chris48s <chris48s@users.noreply.github.com> Date: Tue, 5 May 2020 18:55:22 +0100 Subject: [PATCH] send s-maxage cache header (#5046) Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- core/base-service/cache-headers.js | 4 ++-- core/base-service/cache-headers.spec.js | 8 ++++++-- core/base-service/legacy-request-handler.spec.js | 12 ++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/base-service/cache-headers.js b/core/base-service/cache-headers.js index ba3455fdca..7ab14187c9 100644 --- a/core/base-service/cache-headers.js +++ b/core/base-service/cache-headers.js @@ -69,7 +69,7 @@ function setHeadersForCacheLength(res, cacheLengthSeconds) { cacheControl = 'no-cache, no-store, must-revalidate' expires = nowGMTString } else { - cacheControl = `max-age=${cacheLengthSeconds}` + cacheControl = `max-age=${cacheLengthSeconds} s-maxage=${cacheLengthSeconds}` expires = new Date(now.getTime() + cacheLengthSeconds * 1000).toGMTString() } @@ -94,7 +94,7 @@ function setCacheHeaders({ setHeadersForCacheLength(res, cacheLengthSeconds) } -const staticCacheControlHeader = `max-age=${24 * 3600}` // 1 day. +const staticCacheControlHeader = `max-age=${24 * 3600} s-maxage=${24 * 3600}` // 1 day. function setCacheHeadersForStaticResource(res) { res.setHeader('Cache-Control', staticCacheControlHeader) res.setHeader('Last-Modified', serverStartTimeGMTString) diff --git a/core/base-service/cache-headers.spec.js b/core/base-service/cache-headers.spec.js index ebe4e61df5..404c820b17 100644 --- a/core/base-service/cache-headers.spec.js +++ b/core/base-service/cache-headers.spec.js @@ -146,7 +146,9 @@ describe('Cache header functions', function() { }) it('should set the expected Cache-Control header', function() { - expect(res._headers['cache-control']).to.equal('max-age=123') + expect(res._headers['cache-control']).to.equal( + 'max-age=123 s-maxage=123' + ) }) it('should set the expected Expires header', function() { @@ -184,7 +186,9 @@ describe('Cache header functions', function() { }) it('should set the expected Cache-Control header', function() { - expect(res._headers['cache-control']).to.equal(`max-age=${24 * 3600}`) + expect(res._headers['cache-control']).to.equal( + `max-age=${24 * 3600} s-maxage=${24 * 3600}` + ) }) it('should set the expected Last-Modified header', function() { diff --git a/core/base-service/legacy-request-handler.spec.js b/core/base-service/legacy-request-handler.spec.js index 521a8889f1..d13614a44b 100644 --- a/core/base-service/legacy-request-handler.spec.js +++ b/core/base-service/legacy-request-handler.spec.js @@ -254,7 +254,7 @@ describe('The request handler', function() { +new Date(headers.date) + 900000 ).toGMTString() expect(headers.expires).to.equal(expectedExpiry) - expect(headers['cache-control']).to.equal('max-age=900') + expect(headers['cache-control']).to.equal('max-age=900 s-maxage=900') }) it('should set the expected cache headers on cached responses', async function() { @@ -268,7 +268,7 @@ describe('The request handler', function() { +new Date(headers.date) + 900000 ).toGMTString() expect(headers.expires).to.equal(expectedExpiry) - expect(headers['cache-control']).to.equal('max-age=900') + expect(headers['cache-control']).to.equal('max-age=900 s-maxage=900') }) it('should let live service data override the default cache headers with longer value', async function() { @@ -289,7 +289,7 @@ describe('The request handler', function() { ) const { headers } = await got(`${baseUrl}/testing/123.json`) - expect(headers['cache-control']).to.equal('max-age=400') + expect(headers['cache-control']).to.equal('max-age=400 s-maxage=400') }) it('should not let live service data override the default cache headers with shorter value', async function() { @@ -310,7 +310,7 @@ describe('The request handler', function() { ) const { headers } = await got(`${baseUrl}/testing/123.json`) - expect(headers['cache-control']).to.equal('max-age=300') + expect(headers['cache-control']).to.equal('max-age=300 s-maxage=300') }) it('should set the expires header to current time + cacheSeconds', async function() { @@ -322,7 +322,7 @@ describe('The request handler', function() { +new Date(headers.date) + 3600000 ).toGMTString() expect(headers.expires).to.equal(expectedExpiry) - expect(headers['cache-control']).to.equal('max-age=3600') + expect(headers['cache-control']).to.equal('max-age=3600 s-maxage=3600') }) it('should ignore cacheSeconds when shorter than defaultCacheLengthSeconds', async function() { @@ -334,7 +334,7 @@ describe('The request handler', function() { +new Date(headers.date) + 600000 ).toGMTString() expect(headers.expires).to.equal(expectedExpiry) - expect(headers['cache-control']).to.equal('max-age=600') + expect(headers['cache-control']).to.equal('max-age=600 s-maxage=600') }) it('should set Cache-Control: no-cache, no-store, must-revalidate if cache seconds is 0', async function() { -- GitLab