Skip to content

fix: Wrong logic for render decorator #1147

Closed
@shushanfx

Description

@shushanfx

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())
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions