Skip to content
Snippets Groups Projects
Unverified Commit 1c736f21 authored by chris48s's avatar chris48s Committed by GitHub
Browse files

send s-maxage cache header (#5046)

parent 00595996
No related branches found
No related tags found
No related merge requests found
...@@ -69,7 +69,7 @@ function setHeadersForCacheLength(res, cacheLengthSeconds) { ...@@ -69,7 +69,7 @@ function setHeadersForCacheLength(res, cacheLengthSeconds) {
cacheControl = 'no-cache, no-store, must-revalidate' cacheControl = 'no-cache, no-store, must-revalidate'
expires = nowGMTString expires = nowGMTString
} else { } else {
cacheControl = `max-age=${cacheLengthSeconds}` cacheControl = `max-age=${cacheLengthSeconds} s-maxage=${cacheLengthSeconds}`
expires = new Date(now.getTime() + cacheLengthSeconds * 1000).toGMTString() expires = new Date(now.getTime() + cacheLengthSeconds * 1000).toGMTString()
} }
...@@ -94,7 +94,7 @@ function setCacheHeaders({ ...@@ -94,7 +94,7 @@ function setCacheHeaders({
setHeadersForCacheLength(res, cacheLengthSeconds) 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) { function setCacheHeadersForStaticResource(res) {
res.setHeader('Cache-Control', staticCacheControlHeader) res.setHeader('Cache-Control', staticCacheControlHeader)
res.setHeader('Last-Modified', serverStartTimeGMTString) res.setHeader('Last-Modified', serverStartTimeGMTString)
......
...@@ -146,7 +146,9 @@ describe('Cache header functions', function() { ...@@ -146,7 +146,9 @@ describe('Cache header functions', function() {
}) })
it('should set the expected Cache-Control header', 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() { it('should set the expected Expires header', function() {
...@@ -184,7 +186,9 @@ describe('Cache header functions', function() { ...@@ -184,7 +186,9 @@ describe('Cache header functions', function() {
}) })
it('should set the expected Cache-Control header', 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() { it('should set the expected Last-Modified header', function() {
......
...@@ -254,7 +254,7 @@ describe('The request handler', function() { ...@@ -254,7 +254,7 @@ describe('The request handler', function() {
+new Date(headers.date) + 900000 +new Date(headers.date) + 900000
).toGMTString() ).toGMTString()
expect(headers.expires).to.equal(expectedExpiry) 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() { it('should set the expected cache headers on cached responses', async function() {
...@@ -268,7 +268,7 @@ describe('The request handler', function() { ...@@ -268,7 +268,7 @@ describe('The request handler', function() {
+new Date(headers.date) + 900000 +new Date(headers.date) + 900000
).toGMTString() ).toGMTString()
expect(headers.expires).to.equal(expectedExpiry) 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() { 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() { ...@@ -289,7 +289,7 @@ describe('The request handler', function() {
) )
const { headers } = await got(`${baseUrl}/testing/123.json`) 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() { 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() { ...@@ -310,7 +310,7 @@ describe('The request handler', function() {
) )
const { headers } = await got(`${baseUrl}/testing/123.json`) 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() { it('should set the expires header to current time + cacheSeconds', async function() {
...@@ -322,7 +322,7 @@ describe('The request handler', function() { ...@@ -322,7 +322,7 @@ describe('The request handler', function() {
+new Date(headers.date) + 3600000 +new Date(headers.date) + 3600000
).toGMTString() ).toGMTString()
expect(headers.expires).to.equal(expectedExpiry) 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() { it('should ignore cacheSeconds when shorter than defaultCacheLengthSeconds', async function() {
...@@ -334,7 +334,7 @@ describe('The request handler', function() { ...@@ -334,7 +334,7 @@ describe('The request handler', function() {
+new Date(headers.date) + 600000 +new Date(headers.date) + 600000
).toGMTString() ).toGMTString()
expect(headers.expires).to.equal(expectedExpiry) 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() { it('should set Cache-Control: no-cache, no-store, must-revalidate if cache seconds is 0', async function() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment