refactor: some cache-control headers related

This commit is contained in:
Bobby Wibowo 2022-07-12 06:27:02 +07:00
parent af754d7d71
commit 21d75f71f3
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF

View File

@ -126,7 +126,9 @@ if (!isDevMode && Array.isArray(config.rateLimits) && config.rateLimits.length)
*/ */
const cdnPages = [...config.pages] const cdnPages = [...config.pages]
let setHeaders
// Defaults to no-op
let setHeadersForStaticAssets = () => {}
const contentTypes = typeof config.overrideContentTypes === 'object' && const contentTypes = typeof config.overrideContentTypes === 'object' &&
Object.keys(config.overrideContentTypes) Object.keys(config.overrideContentTypes)
@ -211,8 +213,10 @@ if (config.cacheControl) {
// By default soft cache everything // By default soft cache everything
safe.use('/', (req, res, next) => { safe.use('/', (req, res, next) => {
// FIXME: Routes further down the line that may set their own Cache-Control headers,
// will end up with multiple headers
res.set('Cache-Control', cacheControls.validate) res.set('Cache-Control', cacheControls.validate)
next() return next()
}) })
switch (config.cacheControl) { switch (config.cacheControl) {
@ -248,19 +252,19 @@ if (config.cacheControl) {
// Function for static assets. // Function for static assets.
// This requires the assets to use version in their query string, // This requires the assets to use version in their query string,
// as they will be cached by clients for a very long time. // as they will be cached by clients for a very long time.
setHeaders = res => { setHeadersForStaticAssets = (req, res) => {
res.set('Cache-Control', cacheControls.static) res.set('Cache-Control', cacheControls.static)
} }
// Consider album ZIPs static as well, since they use version in their query string // Consider album ZIPs static as well, since they use version in their query string
safe.use(['/api/album/zip'], (req, res, next) => { safe.use('/api/album/zip', (req, res, next) => {
const versionString = parseInt(req.query.v) const versionString = parseInt(req.query.v)
if (versionString > 0) { if (versionString > 0) {
res.set('Cache-Control', cacheControls.static) res.set('Cache-Control', cacheControls.static)
} else { } else {
res.set('Cache-Control', cacheControls.disable) res.set('Cache-Control', cacheControls.disable)
} }
next() return next()
}) })
} else if (config.serveFilesWithNode) { } else if (config.serveFilesWithNode) {
const opts = {} const opts = {}
@ -276,18 +280,18 @@ const liveDirectoryPublic = initLiveDirectory({ path: paths.public })
const liveDirectoryDist = initLiveDirectory({ path: paths.dist }) const liveDirectoryDist = initLiveDirectory({ path: paths.dist })
safe.use('/', (req, res, next) => { safe.use('/', (req, res, next) => {
// Only process GET and HEAD requests // Only process GET and HEAD requests
if (req.method !== 'GET' && req.method !== 'HEAD') { if (req.method === 'GET' || req.method === 'HEAD') {
return next()
}
// Try to find asset from public directory, then dist directory // Try to find asset from public directory, then dist directory
const file = liveDirectoryPublic.get(req.path) || liveDirectoryDist.get(req.path) const file =
liveDirectoryPublic.get(req.path) ||
liveDirectoryDist.get(req.path)
if (file === undefined) { if (file === undefined) {
return next() return next()
} }
if (typeof setHeaders === 'function') { setHeadersForStaticAssets(req, res)
setHeaders(res)
}
return res.type(file.extension).send(file.buffer) return res.type(file.extension).send(file.buffer)
}
return next()
}) })
// Routes // Routes