Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache text only static badges for longer #10403

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/base-service/base-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions core/base-service/cache-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions core/base-service/redirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion core/server/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
Loading