mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 20:42:43 +00:00
* Enable caching of static assets * Remove dependency on mime package Since we only care about two file extensions at the moment, there is no need to grab the whole package just to avoid calling 'endsWith' twice.
32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
const expressStaticGzip = require('express-static-gzip');
|
|
|
|
// Serve brotli-compressed static files if available
|
|
const customCacheControlHandler=(response, path)=>{
|
|
if(path.endsWith('.br')) {
|
|
// Drop .br suffix to help mime understand the actual type of the file
|
|
path = path.slice(0, -3);
|
|
}
|
|
if(path.endsWith('.js') || path.endsWith('.css')) {
|
|
// .js and .css files are allowed to be cached up to 12 hours, but then
|
|
// they must be revalidated to see if there are any updates
|
|
response.setHeader('Cache-Control', 'public, max-age: 43200, must-revalidate');
|
|
} else {
|
|
// Everything else is cached up to a months as we don't update our images
|
|
// or fonts frequently
|
|
response.setHeader('Cache-Control', 'public, max-age=2592000, must-revalidate');
|
|
}
|
|
};
|
|
|
|
const init=(pathToAssets)=>{
|
|
return expressStaticGzip(pathToAssets, {
|
|
enableBrotli : true,
|
|
orderPreference : ['br'],
|
|
index : false,
|
|
serveStatic : {
|
|
cacheControl : false, // we are going to use custom cache-control
|
|
setHeaders : customCacheControlHandler
|
|
} });
|
|
};
|
|
|
|
module.exports = init;
|