set 'manul-purge' surrogate key on assets/cb- URLs (#25028)

This commit is contained in:
Peter Bengtsson 2022-02-04 11:55:40 -05:00 коммит произвёл GitHub
Родитель 5252288755
Коммит f33ab1cf89
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 28 добавлений и 6 удалений

Просмотреть файл

@ -15,7 +15,7 @@ import handleCsrfErrors from './handle-csrf-errors.js'
import compression from 'compression'
import {
setDefaultFastlySurrogateKey,
setManualFastlySurrogateKey,
setManualFastlySurrogateKeyIfChecksummed,
} from './set-fastly-surrogate-key.js'
import setFastlyCacheHeaders from './set-fastly-cache-headers.js'
import catchBadAcceptLanguage from './catch-bad-accept-language.js'
@ -115,15 +115,21 @@ export default function (app) {
app.use(favicon)
// Any `/assets/cb-*` request should get the setManualFastlySurrogateKey()
// middleware, but it's not possible to express such a prefix in
// Express middlewares. Because we don't want the manual Fastly
// surrogate key on *all* /assets/ requests.
// Note, this needs to come before `assetPreprocessing` because
// the `assetPreprocessing` middleware will rewrite `req.url` if
// it applies.
app.use(setManualFastlySurrogateKeyIfChecksummed)
// Must come before any other middleware for assets
app.use(archivedAssetRedirects)
// This must come before the express.static('assets') middleware.
app.use(assetPreprocessing)
// By specifying '/assets/cb-' and not just '/assets/' we
// avoid possibly legacy enterprise assets URLs and asset image URLs
// that don't have the cache-busting piece in it.
app.use('/assets/cb-', setManualFastlySurrogateKey)
app.use(
'/assets/',
express.static('assets', {

Просмотреть файл

@ -32,3 +32,10 @@ export function setDefaultFastlySurrogateKey(req, res, next) {
res.set(KEY, SURROGATE_ENUMS.DEFAULT)
return next()
}
export function setManualFastlySurrogateKeyIfChecksummed(req, res, next) {
if (req.path.startsWith('/assets/cb-')) {
return setManualFastlySurrogateKey(req, res, next)
}
return next()
}

Просмотреть файл

@ -1034,7 +1034,16 @@ describe('static routes', () => {
expect(res.headers['set-cookie']).toBeUndefined()
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=\d+/)
expect(res.headers['surrogate-key']).toBeTruthy()
expect(res.headers['surrogate-key']).toBe(SURROGATE_ENUMS.MANUAL)
})
it('no manual surrogate key for /assets requests without caching-busting prefix', async () => {
const res = await get('/assets/images/site/be-social.gif')
expect(res.statusCode).toBe(200)
expect(res.headers['set-cookie']).toBeUndefined()
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=\d+/)
expect(res.headers['surrogate-key']).toBe(SURROGATE_ENUMS.DEFAULT)
})
it('serves schema files from the /data/graphql directory at /public', async () => {