Add the possibility to asynchroniously obtain templates
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 8.1k
- Forks
- 846
- PR merge metrics
- No merged PRs in 30d
Description
Hello everyone,
I use ejs for both server and client side rendering.
But I found out that the clientside functions (eventhough templates can be compiled with async: true) are not really able to handle asynchronous 'include' functions.
So there are a few options to modify the behavior the 'include' function to make it situation-fit.
-
On the server side there is
ejs.fileLoaderwhich allows for the actual file-reading to be modified. This won't work because on the client-side there is no access to any fileSystem (used prior to theejs.fileLoader. -
There is a
includerproperty inejs.Options, but it is poorly documented and not clearly referenced in the ejs.js file. So I am not sure if it is usable (I assume only server-side as the passed arguments are already refactured paths). -
There is a third argument for the
ejs.AsyncClientFunctionandejs.ClientFunctionwhich can be used client-side for polyfilling theincludefunction.
Only the third option is useable on the clientside as far as I gathered, but its function does not allow asynchronous actions which can be really bad for performance and usability reasons.
In my case I load a subset of templates from the server and use them clientside.
I would be able to do so syncronously with an XMLHttpRequest with the async flag set to false, but that obviously would be a verry bad idea. I am pretty sure this approuch wouldn't be usable when working with Workers either.
In any case there would be a huge benefit to be able to use asyncrhonous functions. I am not sure how/where it is implemented, otherwise I would have tried to create a PR, But the following snippet would allow both sync and async functions to be used as includer.
// suppose something like this would be used to obtain an `include` statement
let includedString = includer(path, data);
// easily refactured to allow both sync and async versions of `includer`, assuming the context is async as well!
let includedString = await Promise.resolve(includer(path. data));
Would love to hear your feedback on this, or any suggestions on how to temp-fix this issue in the meantime!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ejs.js around the AsyncClientFunction, ClientFunction, and includer option paths to determine how client-side includes are currently resolved. Trace whether the async client path can accommodate asynchronous includer results without changing synchronous behavior; done means the requested client-side async include use case is defined and covered by appropriate validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100