mde / mde/ejs

Add the possibility to asynchroniously obtain templates

Open
#708 0 comments 4 reactions 0 assignees View on GitHub

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.

  1. On the server side there is ejs.fileLoader which 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 the ejs.fileLoader.

  2. There is a includer property in ejs.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).

  3. There is a third argument for the ejs.AsyncClientFunction and ejs.ClientFunction which can be used client-side for polyfilling the include function.

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.