@@ -178,12 +178,22 @@ async function* handleRoute(options: {
178178
179179 const parentRoutes = new Map < string , RouteTreeNodeMetadata > ( ) ;
180180
181+ if ( typeof redirectTo === 'string' ) {
182+ let routeSegments : string [ ] | undefined ;
183+ // Reference the route parameters by their position in the URL (see `buildUrlWithParams`).
184+ metadata . redirectTo = resolveRedirectTo ( parentRoute , route , redirectTo , ( parameter ) => {
185+ routeSegments ??= joinUrlParts ( baseHref , currentRoutePath ) . split ( '/' ) ;
186+
187+ return `*${ routeSegments . lastIndexOf ( parameter ) } ` ;
188+ } ) ;
189+ metadata . relativeRedirect = redirectTo [ 0 ] !== '/' ? true : undefined ;
190+ }
191+
181192 if ( metadata . renderMode === RenderMode . Prerender ) {
182193 for await ( const result of handleSSGRoute (
183194 serverConfigRouteTree ,
184195 route ,
185196 parentRoute ,
186- baseHref ,
187197 metadata ,
188198 parentInjector ,
189199 invokeGetPrerenderParams ,
@@ -195,28 +205,16 @@ async function* handleRoute(options: {
195205 parentRoutes . set ( result . route , result ) ;
196206 }
197207 }
198- } else if ( redirectTo !== undefined ) {
199- if ( metadata . status && ! isValidRedirectResponseCode ( metadata . status ) ) {
200- yield {
201- error :
202- `The '${ metadata . status } ' status code is not a valid redirect response code. ` +
203- `Please use one of the following redirect response codes: ${ [ ...VALID_REDIRECT_RESPONSE_CODES . values ( ) ] . join ( ', ' ) } .` ,
204- } ;
205- } else if ( typeof redirectTo === 'string' ) {
206- let routeSegments : string [ ] | undefined ;
207- parentRoutes . set ( metadata . route , {
208- ...metadata ,
209- // Reference the route parameters by their position in the URL (see `buildUrlWithParams`).
210- redirectTo : resolveRedirectTo ( parentRoute , route , redirectTo , ( parameter ) => {
211- routeSegments ??= joinUrlParts ( baseHref , currentRoutePath ) . split ( '/' ) ;
212-
213- return `*${ routeSegments . lastIndexOf ( parameter ) } ` ;
214- } ) ,
215- relativeRedirect : redirectTo [ 0 ] !== '/' ? true : undefined ,
216- } ) ;
217- } else {
218- parentRoutes . set ( metadata . route , metadata ) ;
219- }
208+ } else if (
209+ redirectTo !== undefined &&
210+ metadata . status &&
211+ ! isValidRedirectResponseCode ( metadata . status )
212+ ) {
213+ yield {
214+ error :
215+ `The '${ metadata . status } ' status code is not a valid redirect response code. ` +
216+ `Please use one of the following redirect response codes: ${ [ ...VALID_REDIRECT_RESPONSE_CODES . values ( ) ] . join ( ', ' ) } .` ,
217+ } ;
220218 } else {
221219 parentRoutes . set ( metadata . route , metadata ) ;
222220 }
@@ -433,7 +431,6 @@ function appendPreloadToMetadata(
433431 * @param serverConfigRouteTree - The tree representing the server's routing setup.
434432 * @param route - The route configuration.
435433 * @param parentRoute - The path of the parent route.
436- * @param baseHref - The base href of the application.
437434 * @param metadata - The metadata associated with the route tree node.
438435 * @param parentInjector - The dependency injection container for the parent route.
439436 * @param invokeGetPrerenderParams - A flag indicating whether to invoke the `getPrerenderParams` function.
@@ -444,7 +441,6 @@ async function* handleSSGRoute(
444441 serverConfigRouteTree : RouteTree < ServerConfigRouteTreeAdditionalMetadata > | undefined ,
445442 route : Route ,
446443 parentRoute : string ,
447- baseHref : string ,
448444 metadata : ServerConfigRouteTreeNodeMetadata ,
449445 parentInjector : Injector ,
450446 invokeGetPrerenderParams : boolean ,
@@ -463,18 +459,6 @@ async function* handleSSGRoute(
463459 delete meta [ 'getPrerenderParams' ] ;
464460 }
465461
466- const redirectTo = typeof route . redirectTo === 'string' ? route . redirectTo : undefined ;
467- if ( redirectTo !== undefined ) {
468- let routeSegments : string [ ] | undefined ;
469- // Reference the route parameters by their position in the URL (see `buildUrlWithParams`).
470- meta . redirectTo = resolveRedirectTo ( parentRoute , route , redirectTo , ( parameter ) => {
471- routeSegments ??= joinUrlParts ( baseHref , currentRoutePath ) . split ( '/' ) ;
472-
473- return `*${ routeSegments . lastIndexOf ( parameter ) } ` ;
474- } ) ;
475- meta . relativeRedirect = redirectTo [ 0 ] !== '/' ? true : undefined ;
476- }
477-
478462 const isCatchAllRoute = CATCH_ALL_REGEXP . test ( currentRoutePath ) ;
479463 if (
480464 ( isCatchAllRoute && ! getPrerenderParams ) ||
@@ -516,6 +500,7 @@ async function* handleSSGRoute(
516500 }
517501 }
518502
503+ const redirectTo = typeof route . redirectTo === 'string' ? route . redirectTo : undefined ;
519504 const parameters = await runInInjectionContext ( parentInjector , ( ) => getPrerenderParams ( ) ) ;
520505 try {
521506 for ( const params of parameters ) {
@@ -607,6 +592,10 @@ function resolveRedirectTo(
607592 redirectTo : string ,
608593 resolveParam : ( parameter : string ) => string ,
609594) : string {
595+ if ( redirectTo [ 0 ] === '/' && ( route . matcher || ! redirectTo . includes ( '/:' ) ) ) {
596+ return redirectTo ;
597+ }
598+
610599 const [ path ] = redirectTo . split ( / [ ? # ] / , 1 ) ;
611600 let resolvedPath = path ;
612601
0 commit comments