mde/ejs

Can ejs.renderFile use 'layouts'

Closed this issue · 3 comments

This is a question on EJS and the renderFile.

Currently, with the regular ejs.render we use a 'layout' file, then our 'template' of the page is injected into the body of that layout file. However, with renderFile, it seems to only take the 'single' file with passed arguments to render that file.

The question is, can you actually use the 'layout' file, or is the idea to use a single 'file' vs the 'layout'?

Just to add to this question and maybe get some thoughts on this.
Basically, what we are looking to do is pass the rendering to a 'worker' thread. The main reason for this is to prevent 'blocking' during the page request. As it stands right now, many of our pages pass over an object returned from the DB where we then loop over the results. While this does work, we noticed it blocks the EL.

We then used the async await functions of ejs however it was kind of a mixed bag where the blocking did seem to go away, however, if you had a heavy loop it still would block the EL (not surprising)

This moved us into the direction of using a worker_thread to process the page using a worker pool.

The general flow of this process is to

  1. Pass our data to the worker
  2. Inside the worker we call the EJS renderFile
  3. Pass the HTML back from the worker
  4. Route returns the HTML as a res.send(html)

We have this partially working, in regards to returning the main content are (minus the layout, hence the first part of the question)

Doing it this way allows the blocking to be a non-issue and with our internal testing we can have a 'long' running loop inside the template that does not impact the EL and other users.

The second part to this question is, any downside to doing this with workers? And if layouts are not supported with the renderFile, would just constructing it as 'partials' be the best option, so we would have

  1. Template file
  2. Inside template file use 'includes' for {header} mainPageData {footer} ?

For more clarity on the app, it currently is deployed in containers and we don't see any resource starvation and the EL blocks are small, like 30 MS, however under load that is still not ideal, so off loading that so it does not impact other users is the goal here.

Anyhow, maybe we missed something and this is the totally wrong approach but wanted to see if anyone else experienced things like this and worked around it etc.

Did you able to find any solution? experiencing same

mde commented

Thanks for circling back to respond on this. This is a great solution. Closing this issue.