-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify-middleware.js
31 lines (28 loc) · 932 Bytes
/
minify-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { minify } = require('html-minifier');
function minifyHTMLContent(content) {
return minify(content, {
removeAttributeQuotes: true,
collapseWhitespace: true,
removeComments: true,
minifyCSS: true,
minifyJS: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeOptionalTags: true
});
}
function minifyHTMLMiddleware(req, res, next) {
const originalRender = res.render;
res.render = function(view, options, callback) {
originalRender.call(res, view, options, function(err, html) {
if (err) {
return callback ? callback(err) : next(err);
}
const minifiedHtml = minifyHTMLContent(html);
callback ? callback(null, minifiedHtml) : res.send(minifiedHtml);
});
};
next();
}
module.exports = minifyHTMLMiddleware;