kyle-mccarthy / nest-next

Render Module to add Nextjs support for Nestjs
MIT License
643 stars 91 forks source link

Usage with next-routes #39

Open webberwang opened 4 years ago

webberwang commented 4 years ago

The controller stops working if I use the routes handler.

codelab-ui-nestjs____Code_UIB_codelab-ui-nestjs__-_____packages_core_server_server_ts codelab-ui-nestjs____Code_UIB_codelab-ui-nestjs__-_____packages_core_server_app_app_controller_ts

I'm also unable to access useRouter().query from next/router if I'm using routing with controller

kyle-mccarthy commented 4 years ago

Can you elaborate on what happens when trying to use useRouter?

It sounds like this may be two separate issues, but in general we don’t support other 3rd party routing libraries. nest-next uses nestjs for the routing but you can add a global filter to pass requests that don’t resolve within nest and try to resolve the request with next. See this issue for context https://github.com/kyle-mccarthy/nest-next/issues/38 On Feb 2, 2020, 8:37 PM -0600, Webber Wang notifications@github.com, wrote:

Reopened #39. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

webberwang commented 4 years ago

@kyle-mccarthy

useRouter is just empty object, which is inline with Nest not passing requests to Next.

Right, solving issue 2 (resolving requests not resolved in nest) would solve 1 (external router not working), since we wouldn't need next-routes anymore.

Here's my attempt following #38...

server.ts

@Catch(NotFoundException)
export class NextPageFilter implements ExceptionFilter {
  private requestHandler?: RequestHandler;

  constructor(@Inject() private readonly renderService: RenderService) {
    this.requestHandler = this.renderService.getRequestHandler();
  }

  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const res = ctx.getResponse();
    const req = ctx.getRequest();

    if (this.requestHandler) {
      return this.requestHandler(req, res);
    }

    throw exception;
  }
}

(async () => {
  /**
   * Create server
   */
  const server = Server({
    dev: true,
  });
  await server.prepare();

  /**
   * Create app
   */
  const app = await NestFactory.create(AppModule);

  const renderer = app.get(RenderModule);
  renderer.register(app as any, server);

  /**
   * Add service
   */
  const service = app.get(RenderService);
  app.useGlobalFilters(new NextPageFilter(service));

  await app.listen(3000);
})();

useRouter is still showing blank. I'm not too sure how to pass requests that don’t resolve within nest and try to resolve the request with next. There isn't too much info on the internet for this specific use case.

kyle-mccarthy commented 4 years ago

@webberwang I added a new option in the nest-next beta preview. I think that it may allow for you to use next-routes. See https://github.com/kyle-mccarthy/nest-next/issues/38#issuecomment-647867509