Skip to content

Commit 8cea28a

Browse files
authored
use manual surrogate key for nextjs static assets (github#25096)
* use manual surrogate key for nextjs static assets * refactor
1 parent b4da5f2 commit 8cea28a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

middleware/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import cookieParser from './cookie-parser.js'
1313
import csrf from './csrf.js'
1414
import handleCsrfErrors from './handle-csrf-errors.js'
1515
import compression from 'compression'
16-
import {
17-
setDefaultFastlySurrogateKey,
18-
setManualFastlySurrogateKeyIfChecksummed,
19-
} from './set-fastly-surrogate-key.js'
16+
import { setDefaultFastlySurrogateKey } from './set-fastly-surrogate-key.js'
2017
import setFastlyCacheHeaders from './set-fastly-cache-headers.js'
2118
import catchBadAcceptLanguage from './catch-bad-accept-language.js'
2219
import reqUtils from './req-utils.js'
@@ -63,6 +60,7 @@ import renderPage from './render-page.js'
6360
import assetPreprocessing from './asset-preprocessing.js'
6461
import archivedAssetRedirects from './archived-asset-redirects.js'
6562
import favicon from './favicon.js'
63+
import setStaticAssetCaching from './static-asset-caching.js'
6664

6765
const { DEPLOYMENT_ENV, NODE_ENV } = process.env
6866
const isDevelopment = NODE_ENV === 'development'
@@ -115,14 +113,16 @@ export default function (app) {
115113

116114
app.use(favicon)
117115

118-
// Any `/assets/cb-*` request should get the setManualFastlySurrogateKey()
119-
// middleware, but it's not possible to express such a prefix in
120-
// Express middlewares. Because we don't want the manual Fastly
121-
// surrogate key on *all* /assets/ requests.
122-
// Note, this needs to come before `assetPreprocessing` because
116+
// Any static URL that contains some sort of checksum that makes it
117+
// unique gets the "manual" surrogate key. If it's checksummed,
118+
// it's bound to change when it needs to change. Otherwise,
119+
// we want to make sure it doesn't need to be purged just because
120+
// there's a production deploy.
121+
// Note, for `/assets/cb-*...` requests,
122+
// this needs to come before `assetPreprocessing` because
123123
// the `assetPreprocessing` middleware will rewrite `req.url` if
124124
// it applies.
125-
app.use(setManualFastlySurrogateKeyIfChecksummed)
125+
app.use(setStaticAssetCaching)
126126

127127
// Must come before any other middleware for assets
128128
app.use(archivedAssetRedirects)

middleware/set-fastly-surrogate-key.js

-12
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,7 @@ export function setFastlySurrogateKey(res, enumKey) {
2323
res.set(KEY, enumKey)
2424
}
2525

26-
export function setManualFastlySurrogateKey(req, res, next) {
27-
res.set(KEY, SURROGATE_ENUMS.MANUAL)
28-
return next()
29-
}
30-
3126
export function setDefaultFastlySurrogateKey(req, res, next) {
3227
res.set(KEY, SURROGATE_ENUMS.DEFAULT)
3328
return next()
3429
}
35-
36-
export function setManualFastlySurrogateKeyIfChecksummed(req, res, next) {
37-
if (req.path.startsWith('/assets/cb-')) {
38-
return setManualFastlySurrogateKey(req, res, next)
39-
}
40-
return next()
41-
}

middleware/static-asset-caching.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
2+
3+
export default function setStaticAssetCaching(req, res, next) {
4+
if (isChecksummed(req.path)) {
5+
setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL)
6+
}
7+
return next()
8+
}
9+
10+
// True if the URL is known to contain some pattern of a checksum that
11+
// would make it intelligently different if its content has changed.
12+
function isChecksummed(path) {
13+
if (path.startsWith('/assets/cb-')) return true
14+
if (path.startsWith('/_next/static') && /[a-f0-9]{20}/.test(path)) return true
15+
return false
16+
}

0 commit comments

Comments
 (0)