0
votes

I'm currently rendering a handlebars templates to display pages on my node server. I've set the view engine to handlebars

res.render('product', vars);

However, I would like to run the compiled HTML template through juice2 so that the styles are inlined. Because the page is rendered on request, I cannot format the HTML before being sent to the client.

Is there any way of rendering the template server side (to a variable preferably, formatting the content in juice2 and then outputting them)

1

1 Answers

1
votes

You can do something like this:

res.render('product', vars, function(err, html) {
    juice.juiceContent(html, options, function(err, finalHTML) {
        res.send(finalHTML); 
    })
});
// options.url = base domain like http://example.com

References:

  1. https://www.npmjs.com/package/juice2#juicejuicecontenthtml-options-callback
  2. http://expressjs.com/en/api.html#res