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

Cache text only static badges for longer (#10403)

* cache text only static badges for longer

* explaining cache-control header on redirects more clearly
parent f99fd17a
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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)
}
......
......@@ -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()
......
......@@ -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')
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment