1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/middleware/asset-preprocessing.js
Peter Bengtsson ae3dadfc66 cache asset images more aggressively (#23553)
* cache asset images more aggressively

* more careful about which gets the manual surrogate key

* fix rendered-content-link-checker script too

* feedbacked
2021-12-10 13:01:48 +00:00

27 lines
1.0 KiB
JavaScript

// This middleware rewrites the URL of requests that contain the
// portion of `/cb-\d+/`.
// "cb" stands for "cache bust".
// There's a Markdown plugin that rewrites all <img src> values
// from `<img src="/assets/foo/bar.png">` to
// `<img src="/assets/cb-123467/foo/bar.png">` for example.
// We're doing this so that we can set a much more aggressive
// Cache-Control for assets and a CDN surrogate-key that doesn't
// soft-purge on every deployment.
const regex = /\/cb-\d+\//
export default function assetPreprocessing(req, res, next) {
if (req.path.startsWith('/assets/')) {
// We're only confident enough to set the *manual* surrogate key if the
// asset contains the cache-busting piece.
if (regex.test(req.url)) {
// We remove it so that when `express.static()` runs, it can
// find the file on disk by its original name.
req.url = req.url.replace(regex, '/')
// The Cache-Control is managed by the configuration
// for express.static() later in the middleware.
}
}
return next()
}