问题
When using jade-lang on production, would I benefit from having some form of a middleware that pre-compiles all the .jade views and then uses them in res.render? Or does that automatically happen when you do NODE_ENV=production?
I'm simply exploring options on how to speed-up jade rendering on production.
回答1:
When Jade compiles the template, the template is cached. In production environment if you warm up the cache, then there is no need to pre-compile template. Even if you don't, the template will be cached after its first compilation.
I recommend you to have a look Jade's source code to better understand how it works.
exports.render = function(str, options, fn){
// ...
var path = options.filename;
var tmpl = options.cache
? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
: exports.compile(str, options);
return tmpl(options);
};
Source: https://github.com/visionmedia/jade/blob/1.3.0/lib/jade.js#L255-L259
exports.renderFile = function(path, options, fn){
// ...
options.filename = path;
var str = options.cache
? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
: fs.readFileSync(path, 'utf8');
return exports.render(str, options);
};
Source: https://github.com/visionmedia/jade/blob/1.3.0/lib/jade.js#L291-L295
来源:https://stackoverflow.com/questions/22220839/would-it-benefit-to-pre-compile-jade-templates-on-production-in-express