Node.js / Jade - How to precompile jade files and cache them?

Frame: node.js / express.js / Jade

Question: in the production of env, when the jade file is issued by the jade express cache, so future renderings are faster.

When I run the node.js application, how can I precompile (or) pre-render (e.g. warm up) all the jade files so that it is already in the cache when requests start to come in ...

I can use folder recursion, I just need to know how to precompile (or) pre-render.

Is it possible?

+7
pug express
source share
3 answers

Jade has pre-compilation and template caching.

http://jade-lang.com/api/

Just specify cache: true option jade.compileFile and repeat all the template files.

 var options = {cache: true}; // iterate/recurse over your jade template files and compile them jade.compileFile('./templates/foo.jade', options); // Jade will load the compiled templates from cache (the file path is the key) jade.renderFile('./templates/foo.jade'); 
+6
source share

If you don’t use any parameters, you can compile jade templates directly in HTML using grunt or gulp and make it monitor file changes

Try it on the command line: jade view/map-beacons.jade -D

If you need to use parameters, I would use something like Andrew Lavers answer.

compileFile returns a function that you can use to pass in parameters ie fn({ myJsVar: 'someValue' })

The command line also has a client option, but I did not find any benefit for this: jade view/map-beacons.jade -cD

+1
source share

I am making this decision, this code is outside of the http.createServer function

 let cache_index=jade.renderFile('index.jade'); 

and in reverse look

 res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end(cache_index); 

when using this server return server return to 1ms but without a solution server return index from 150 ms to 400 ms

result:

Image 1 with cache enter image description here

Image 2 without cache enter image description here

0
source share

All Articles