在快速生产中预先编译玉模板是有益的 [英] Would it benefit to pre-compile jade templates on production in express

查看:98
本文介绍了在快速生产中预先编译玉模板是有益的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在生产中使用jade-lang时,是否可以从某种形式的中间件获得预先编译所有的.jade视图,然后在res.render中使用它们?或者当您执行NODE_ENV =生产时会自动发生?



我只是探索如何在生产中加快玉石渲染的选项。

解决方案

当Jade编译模板时,模板被缓存。在生产环境中,如果您预热缓存,那么就不需要预编译模板了。即使没有,模板将在首次编译之后缓存。



我建议您看看Jade的源代码,以更好地了解它的工作原理。 / p>

  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);
};

资料来源: 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);
};

资料来源: https://github.com/visionmedia/jade/blob/1.3.0/lib/jade.js#L291-L295


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.

解决方案

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

这篇关于在快速生产中预先编译玉模板是有益的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆