Last active
November 25, 2021 22:20
-
-
Save marcusgadbem/ddde8a7ad302134e2c18 to your computer and use it in GitHub Desktop.
Middleware to minify HTML output for express template render engines in which supports callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* app/controllers/index.js */ | |
module.exports.index = function(req, res) { | |
res.render('index.html'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* config/middlewares/render-minified.js */ | |
/** | |
* Dependencies | |
*/ | |
var Minifier = require('html-minifier'); | |
/** | |
* Render with minified HTML (express + nunjucks) | |
* Works as a response.method that minifies html string | |
* after nunjucks.render compiles and callback | |
* @param {String} view | |
* @param {Object} options | |
*/ | |
module.exports = exports = function(req, res, next) { | |
res.oldRender = res.render; | |
res.render = function(view, options) { | |
this.oldRender(view, options, function(err, html) { | |
if (err) throw err; | |
html = Minifier.minify(html, { | |
removeComments: true, | |
removeCommentsFromCDATA: true, | |
collapseWhitespace: true, | |
collapseBooleanAttributes: true, | |
removeAttributeQuotes: true, | |
removeEmptyAttributes: true | |
}); | |
res.send(html); | |
}); | |
}; | |
next(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* server.js */ | |
var express = require('express'), | |
nunjucks = require('nunjucks'), | |
renderMinified = require('./app/config/middlewares/render-minified'); | |
var app = express(); | |
app.engine('html', nunjucks.render); | |
nunjucks.configure(env.views, { | |
autoescape: true, | |
express: app | |
}); | |
// new response render which passes html-minifier as callback to express render engine | |
app.use(renderMinified); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global describe, it, before */ | |
/* tests/middlewares/render-minified.js */ | |
/* by @rafaelverger */ | |
var assert = require('assert'), | |
sinon = require('sinon'), | |
renderMinified = require('../../app/config/middlewares/render-minified'); | |
describe('render-minified.js', function(){ | |
var renderSpy, | |
renderNextSpy, | |
resMock; | |
before(function() { | |
renderSpy = sinon.spy(); | |
renderNextSpy = sinon.spy(); | |
resMock = {render: renderSpy}; | |
renderMinified({}, resMock, renderNextSpy); | |
}); | |
it ('ensure response.render was overwritten by renderMinified module', function() { | |
assert(renderSpy !== resMock.render); | |
assert(renderSpy === resMock.oldRender); | |
assert(renderNextSpy.calledOnce); | |
assert(renderNextSpy.calledWithExactly()); | |
}); | |
it('ensure the new render function calls old one', function() { | |
var a = "index.html", | |
b = {foo: 'bar'}; | |
resMock.render(a, b); | |
assert(renderSpy.calledOnce); | |
assert(renderSpy.calledWith(a, b)); | |
}); | |
}); |
MAGO NUNES.
@marcusgadbem confere as alteracoes!
@rafaelverger boa! passou. 👍
Atualizei e coloquei como publico. :)
Thanks for that great middleware.
For line 12 of render-minified.js I has to use if (err) return next(err);
otherwise my error handling didn't work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alteração no override do render do render-minified:
E o teste: