You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been experimenting for two hours or so and I've come up with the following solution that seems to work correctly.
NextJS internally uses req.params when handling dynamic routes, if Nest does not parse the params exactly as Next expects them, then Next will try to render a different, potentially non-existent page
Here's my solution app.controller.ts
@Get('/post*')
@Render('/post/[...slug]')
@UseInterceptors(newCatchAllInterceptor('slug'))asyncsignInStep(
@Param('slug')slugArray: string[],){if(slugArray===undefined){// No slug defined, request path is /post// (...)}constslug=slugArray.join('/')// (..)}
catch-all.interceptor.ts
import{Injectable,NestInterceptor,ExecutionContext,CallHandler}from'@nestjs/common';import{Observable}from'rxjs';importexpressfrom'express';
@Injectable()exportclassCatchAllInterceptorimplementsNestInterceptor{constructor(privatereadonlyname: string){}intercept(context: ExecutionContext,next: CallHandler): Observable<unknown>{constrequest=context.switchToHttp().getRequest<express.Request>();constwildcardParam=request.params['0'];if(wildcardParam){// @ts-expect-error request.params does not expect values to be of type string[], but Next doesrequest.params={[this.name]: wildcardParam.split('/')};}returnnext.handle();}}
This issue is a continuation of #93 and #96. Nested routes like
[...slug]
do not work withnest-next
. Next documentation on such pages - https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes.The text was updated successfully, but these errors were encountered: