diff --git a/core/base-service/base-static.js b/core/base-service/base-static.js index d2523cce547a2d5030a8c13b4bd82ab36dacd6d5..70c9969a176cc736190b811ef41e478840bcd923 100644 --- a/core/base-service/base-static.js +++ b/core/base-service/base-static.js @@ -47,7 +47,11 @@ export default class BaseStaticService extends BaseService { const format = (match.slice(-1)[0] || '.svg').replace(/^\./, '') badgeData.format = format - setCacheHeadersForStaticResource(ask.res) + let maxAge = 24 * 3600 // 1 day + if (!queryParams.logo && !badgeData.isError) { + maxAge = 5 * 24 * 3600 // 5 days + } + setCacheHeadersForStaticResource(ask.res, maxAge) const svg = makeBadge(badgeData) makeSend(format, ask.res, end)(svg) diff --git a/core/base-service/cache-headers.js b/core/base-service/cache-headers.js index 881af81d0426d9045304f253b331c16909512bd7..bbecf6a53742e36adccee1c3e3a2b50ce3b8b420 100644 --- a/core/base-service/cache-headers.js +++ b/core/base-service/cache-headers.js @@ -90,8 +90,11 @@ function setCacheHeaders({ setHeadersForCacheLength(res, cacheLengthSeconds) } -const staticCacheControlHeader = `max-age=${24 * 3600}, s-maxage=${24 * 3600}` // 1 day. -function setCacheHeadersForStaticResource(res) { +function setCacheHeadersForStaticResource( + res, + maxAge = 24 * 3600, // 1 day +) { + const staticCacheControlHeader = `max-age=${maxAge}, s-maxage=${maxAge}` res.setHeader('Cache-Control', staticCacheControlHeader) res.setHeader('Last-Modified', serverStartTimeGMTString) } diff --git a/core/base-service/redirector.js b/core/base-service/redirector.js index 00dabed077153f1a76a6383064edcabaa3e57186..2a1f6b0f984e676a9a3e65894878d8531d11c559 100644 --- a/core/base-service/redirector.js +++ b/core/base-service/redirector.js @@ -111,8 +111,12 @@ export default function redirector(attrs) { ask.res.statusCode = 301 ask.res.setHeader('Location', redirectUrl) - // To avoid caching mistakes for a long time, and to make this simpler - // to reason about, use the same cache semantics as the static badge. + /* To avoid caching mistakes forever + (in the absence of cache control directives that specify otherwise, + 301 redirects are cached without any expiry date) + and to make this simpler to reason about, + use the same cache semantics as the static badge. + */ setCacheHeadersForStaticResource(ask.res) ask.res.end() diff --git a/core/server/server.spec.js b/core/server/server.spec.js index 32c0e04bf892b6aa51150962ff10f6878539a465..741b2e92dad0ab829c3fab644152670df8482519 100644 --- a/core/server/server.spec.js +++ b/core/server/server.spec.js @@ -59,8 +59,17 @@ describe('The server', function () { expect(headers['cache-control']).to.equal('max-age=300, s-maxage=300') }) - it('should serve badges with custom maxAge', async function () { + it('should serve static badges without logo with maxAge=432000', async function () { const { headers } = await got(`${baseUrl}badge/foo-bar-blue`) + expect(headers['cache-control']).to.equal( + 'max-age=432000, s-maxage=432000', + ) + }) + + it('should serve badges with with logo with maxAge=86400', async function () { + const { headers } = await got( + `${baseUrl}badge/foo-bar-blue?logo=javascript`, + ) expect(headers['cache-control']).to.equal('max-age=86400, s-maxage=86400') })