mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-19 01:31:34 +00:00
refactor: some cache-control headers related
This commit is contained in:
parent
af754d7d71
commit
21d75f71f3
36
lolisafe.js
36
lolisafe.js
@ -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
|
||||||
|
const file =
|
||||||
|
liveDirectoryPublic.get(req.path) ||
|
||||||
|
liveDirectoryDist.get(req.path)
|
||||||
|
if (file === undefined) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
setHeadersForStaticAssets(req, res)
|
||||||
|
return res.type(file.extension).send(file.buffer)
|
||||||
}
|
}
|
||||||
// Try to find asset from public directory, then dist directory
|
return next()
|
||||||
const file = liveDirectoryPublic.get(req.path) || liveDirectoryDist.get(req.path)
|
|
||||||
if (file === undefined) {
|
|
||||||
return next()
|
|
||||||
}
|
|
||||||
if (typeof setHeaders === 'function') {
|
|
||||||
setHeaders(res)
|
|
||||||
}
|
|
||||||
return res.type(file.extension).send(file.buffer)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
|
Loading…
Reference in New Issue
Block a user