Skip to content

fix: Wrong logic for render decorator #1147

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

Closed
shushanfx opened this issue May 25, 2023 · 2 comments · Fixed by #1482
Closed

fix: Wrong logic for render decorator #1147

shushanfx opened this issue May 25, 2023 · 2 comments · Fixed by #1482
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@shushanfx
Copy link

Description

I added a @Render() decorator for one of my routes, but other routes were also affected. I debuged into the code, and in KoaDriver, I found this:

    else if (action.renderedTemplate) {
      // if template is set then render it // TODO: not working in koa
      const renderOptions = result && result instanceof Object ? result : {};

      this.koa.use(async function (ctx: any, next: any) {
        await ctx.render(action.renderedTemplate, renderOptions);
      });
    }

This could result in the following two results:

  1. Affect other routes which are not marked with @Render decorator.
  2. Register a middleware for koa for every @Render request.

Solution

I suggest change this code to this.

       else if (action.renderedTemplate) {
            const renderOptions = result && result instanceof Object ? result : {};
            return Promise.resolve(
                    options.context.render(action.renderedTemplate, renderOptions)
                ).then(() => options.next())
        }
@shushanfx shushanfx added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 25, 2023
@Telokis
Copy link
Contributor

Telokis commented Jan 21, 2025

Making the KoaDriver::handleSuccess function async and using

await options.context.render(action.renderedTemplate, renderOptions);

works as expected without shortcircuiting the following processing.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

Successfully merging a pull request may close this issue.

2 participants