I came up with the following solution. It may not be perfect in terms of performance (or maybe it is), but it is the best working method I found up to this moment.
First I tried to add express
middleware (express-minify-html
) but with no success (I tried several ways including various middleware options, but the minification just didn’t happen).
Then I decided to use html-minifier
package directly, so I created lib/modules/apostrophe-templates/index.js
, added 'apostrophe-templates': { minify: true }
to data/local.js
and then overrode renderBody
method like this:
const minify = require('html-minifier').minify;
const minifyOptions = {
collapseWhitespace: true,
conservativeCollapse: true,
preserveLineBreaks: true
};
module.exports = {
construct: function (self, options) {
if (options.minify) {
const superRenderBody = self.renderBody;
self.renderBody = function (req, type, s, data, module) {
const body = superRenderBody(req, type, s, data, module);
return self.renderDepth === 0 ? minify(body, minifyOptions) : body;
};
}
}
};
This method works!