nest-next icon indicating copy to clipboard operation
nest-next copied to clipboard

Usage with next-routes

Open webberwang opened this issue 5 years ago • 3 comments

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

webberwang avatar Feb 03 '20 01:02 webberwang

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 [email protected], 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.

kyle-mccarthy avatar Feb 03 '20 03:02 kyle-mccarthy

@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.

webberwang avatar Feb 03 '20 05:02 webberwang

@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

kyle-mccarthy avatar Jun 23 '20 02:06 kyle-mccarthy