Skip to content

Commit

Permalink
use manual surrogate key for nextjs static assets (github#25096)
Browse files Browse the repository at this point in the history
* use manual surrogate key for nextjs static assets

* refactor
  • Loading branch information
peterbe authored Feb 8, 2022
1 parent b4da5f2 commit 8cea28a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
20 changes: 10 additions & 10 deletions middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import cookieParser from './cookie-parser.js'
import csrf from './csrf.js'
import handleCsrfErrors from './handle-csrf-errors.js'
import compression from 'compression'
import {
setDefaultFastlySurrogateKey,
setManualFastlySurrogateKeyIfChecksummed,
} from './set-fastly-surrogate-key.js'
import { setDefaultFastlySurrogateKey } from './set-fastly-surrogate-key.js'
import setFastlyCacheHeaders from './set-fastly-cache-headers.js'
import catchBadAcceptLanguage from './catch-bad-accept-language.js'
import reqUtils from './req-utils.js'
Expand Down Expand Up @@ -63,6 +60,7 @@ import renderPage from './render-page.js'
import assetPreprocessing from './asset-preprocessing.js'
import archivedAssetRedirects from './archived-asset-redirects.js'
import favicon from './favicon.js'
import setStaticAssetCaching from './static-asset-caching.js'

const { DEPLOYMENT_ENV, NODE_ENV } = process.env
const isDevelopment = NODE_ENV === 'development'
Expand Down Expand Up @@ -115,14 +113,16 @@ 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
// Any static URL that contains some sort of checksum that makes it
// unique gets the "manual" surrogate key. If it's checksummed,
// it's bound to change when it needs to change. Otherwise,
// we want to make sure it doesn't need to be purged just because
// there's a production deploy.
// Note, for `/assets/cb-*...` requests,
// this needs to come before `assetPreprocessing` because
// the `assetPreprocessing` middleware will rewrite `req.url` if
// it applies.
app.use(setManualFastlySurrogateKeyIfChecksummed)
app.use(setStaticAssetCaching)

// Must come before any other middleware for assets
app.use(archivedAssetRedirects)
Expand Down
12 changes: 0 additions & 12 deletions middleware/set-fastly-surrogate-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,7 @@ export function setFastlySurrogateKey(res, enumKey) {
res.set(KEY, enumKey)
}

export function setManualFastlySurrogateKey(req, res, next) {
res.set(KEY, SURROGATE_ENUMS.MANUAL)
return next()
}

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()
}
16 changes: 16 additions & 0 deletions middleware/static-asset-caching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'

export default function setStaticAssetCaching(req, res, next) {
if (isChecksummed(req.path)) {
setFastlySurrogateKey(res, SURROGATE_ENUMS.MANUAL)
}
return next()
}

// True if the URL is known to contain some pattern of a checksum that
// would make it intelligently different if its content has changed.
function isChecksummed(path) {
if (path.startsWith('/assets/cb-')) return true
if (path.startsWith('/_next/static') && /[a-f0-9]{20}/.test(path)) return true
return false
}

0 comments on commit 8cea28a

Please sign in to comment.