Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixed async helpers nested in template helpers #10750

Merged
merged 2 commits into from
Jun 25, 2019

Conversation

allouis
Copy link
Contributor

@allouis allouis commented May 20, 2019

closes #10643

The async resolver in express-hbs relies on storing the state of the
promises on the this value inside of a helper, which is always set to
the context. This patch updates our helpers which render templates, to
use this as the context when rendering their templates.

@allouis
Copy link
Contributor Author

allouis commented May 20, 2019

This PR updates the navigation and pagination helpers to forward the context from the helper to the underlying template. So it does technically change the functionality, both by providing more context to the sub template, and modifying the context at the level of the helper.

I'm wondering if the current way was intentional, and this change would be a regression @ErisDS ?

  1. The reason we make the change is because express-hbs stores a "resolverCache" to look up async values from.

  2. The cache is created at the top level and stored on this or context to be passed down to nested templates.

    2.a. It will never be stored on options AFAICT because always one of context or this will exist_

  3. When we render the templates, we don't forward the context or the this value.

  4. The sub template appends promises to be resolves to a resolverCache that the top level has no idea about.

If this is a regression, we can fix it in express-hbs, but either way, the implementation for async helpers in express-hbs is pretty brittle, and I think needs a refresh to stop something like this cropping up again.

@allouis allouis marked this pull request as ready for review May 20, 2019 11:48
@ErisDS
Copy link
Member

ErisDS commented May 22, 2019

Nothing intentional, but IMO using .merge/.extends to override the first variable AND assigning is an antipattern due as it looks like the first variable has been unexpectedly overridden. IMO should be one or the other.

@allouis
Copy link
Contributor Author

allouis commented Jun 10, 2019

@ErisDS I've updated to simply assign to this rather than _.merge and assign to new identifier.

@ErisDS
Copy link
Member

ErisDS commented Jun 10, 2019 via email

@allouis
Copy link
Contributor Author

allouis commented Jun 10, 2019

@ErisDS Rebased to reinstate this functionality

@allouis allouis force-pushed the update-express-hbs branch 3 times, most recently from 000d65d to 0ed228b Compare June 25, 2019 08:54
closes TryGhost#10643

The async resolver in express-hbs relies on storing the state of the
promises on the `this` value inside of a helper, which is always set to
the `context`. This patch updates our helpers which render templates, to
use `this` as the context when rendering their templates.
no-issue

ronseal.
@allouis allouis merged commit da3f55d into TryGhost:master Jun 25, 2019
@allouis allouis deleted the update-express-hbs branch June 25, 2019 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

express-hbs 2.0.2 causes infinite loops and crashes Ghost
2 participants