mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 12:02:48 +00:00
Enable caching of static assets (#1217)
* 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.
This commit is contained in:
12
package-lock.json
generated
12
package-lock.json
generated
@@ -4321,6 +4321,12 @@
|
||||
"pify": "^4.0.1",
|
||||
"semver": "^5.6.0"
|
||||
}
|
||||
},
|
||||
"mime": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
|
||||
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -4570,12 +4576,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"mime": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
|
||||
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
|
||||
"optional": true
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.44.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
const _ = require('lodash');
|
||||
const jwt = require('jwt-simple');
|
||||
const expressStaticGzip = require('express-static-gzip');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
const homebrewApi = require('./server/homebrew.api.js');
|
||||
const GoogleActions = require('./server/googleActions.js');
|
||||
const serveCompressedStaticAssets = require('./server/static-assets.mv.js');
|
||||
|
||||
// Serve brotli-compressed static files if available
|
||||
app.use('/', expressStaticGzip(`${__dirname}/build`, {
|
||||
enableBrotli : true,
|
||||
orderPreference : ['br'],
|
||||
index : false
|
||||
}));
|
||||
app.use('/', serveCompressedStaticAssets(`${__dirname}/build`));
|
||||
|
||||
process.chdir(__dirname);
|
||||
|
||||
|
||||
31
server/static-assets.mv.js
Normal file
31
server/static-assets.mv.js
Normal file
@@ -0,0 +1,31 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user