Would it benefit to pre-compile jade templates on production in express

后端 未结 1 902
灰色年华
灰色年华 2021-01-12 11:53

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 tha

相关标签:
1条回答
  • 2021-01-12 12:29

    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

    0 讨论(0)
提交回复
热议问题