this is an Apostrophe CMS question entirely. Piggy-backing off this question, which was never answered, I decided to ask my question here on Stack Overflow. I could not find the topic here.
https://forum.apostrophecms.org/t/performance-engineering/61/2
With that in mind, ApostropheCMS is a very cool in-editor CMS that is built on an express server, but I can not figure out how to access what would be, in a typical express setup, the app.js file.
This npm module does exactly what we need to implement. https://www.npmjs.com/package/express-static-gzip
The code to add to express:
var express = require('express');
var expressStaticGzip = require('express-static-gzip');
var app = express();
app.use('/', expressStaticGzip('/my/rootFolder/', {
enableBrotli: true,
customCompressions: [{
encodingName: 'deflate',
fileExtension: 'zz'
}],
orderPreference: ['br']
}));
1) How can I add this to a standard Apostrophe setup? or 2) Is there already a method built into apostropheCMS that enables brotli and gzip?
First, Node.js code is not the best place to do this. You will get better performance if you implement it in production via your production reverse proxy, such as nginx, and do not implement it in dev at all. Since running Node behind a proxy is always best practice, that should be a viable option for you.
However, that being said, this can be done in Node too. And perhaps you have a use case like allowing pre-zipped files to be served in this way whether the proxy is present or not.
The servePublicAssets
method of the apostrophe-assets
module is responsible for serving /public
via express.static
. You can change it out at project level:
// in your PROJECT LEVEL lib/modules/apostrophe-assets/index.js file.
// DO NOT modify node_modules/apostrophe, you do not need to do that.
// DO NOT copy the entire index.js.
// This is all you need to override just this ONE method.
const expressStaticGzip = require('express-static-gzip');
module.exports = {
construct: function(self, options) {
self.servePublicAssets = function() {
const middleware = [];
if (self.lessMiddleware) {
middleware.push(self.lessMiddleware);
}
middleware.push(expressStaticGzip(
self.apos.rootDir + '/public',
{
enableBrotli: true,
customCompressions: [
{
encodingName: 'deflate',
fileExtension: 'zz'
}
],
orderPreference: ['br']
}
));
self.expressMiddleware = {
when: 'beforeRequired',
middleware: middleware
};
};
}
};
We do override an entire method here rather than just injecting different middleware. It would be nice if Apostrophe didn't assume you wanted express.static
but rather consulted an option allowing you to inject alternative middleware. That would make a good PR.