@@ -152,25 +152,27 @@ async function* traverseRoutesConfig(options: {
152152 delete metadata . presentInClientRouter ;
153153
154154 // Handle redirects
155- if ( typeof redirectTo === 'string' ) {
156- const redirectToResolved = resolveRedirectTo ( currentRoutePath , redirectTo ) ;
155+ if ( metadata . renderMode === RenderMode . Prerender ) {
156+ // Handle SSG routes
157+ yield * handleSSGRoute (
158+ metadata ,
159+ typeof redirectTo === 'string' ? redirectTo : undefined ,
160+ parentInjector ,
161+ invokeGetPrerenderParams ,
162+ includePrerenderFallbackRoutes ,
163+ ) ;
164+ } else if ( typeof redirectTo === 'string' ) {
157165 if ( metadata . status && ! VALID_REDIRECT_RESPONSE_CODES . has ( metadata . status ) ) {
158166 yield {
159167 error :
160168 `The '${ metadata . status } ' status code is not a valid redirect response code. ` +
161169 `Please use one of the following redirect response codes: ${ [ ...VALID_REDIRECT_RESPONSE_CODES . values ( ) ] . join ( ', ' ) } .` ,
162170 } ;
171+
163172 continue ;
164173 }
165- yield { ...metadata , redirectTo : redirectToResolved } ;
166- } else if ( metadata . renderMode === RenderMode . Prerender ) {
167- // Handle SSG routes
168- yield * handleSSGRoute (
169- metadata ,
170- parentInjector ,
171- invokeGetPrerenderParams ,
172- includePrerenderFallbackRoutes ,
173- ) ;
174+
175+ yield { ...metadata , redirectTo } ;
174176 } else {
175177 yield metadata ;
176178 }
@@ -215,13 +217,15 @@ async function* traverseRoutesConfig(options: {
215217 * all parameterized paths, returning any errors encountered.
216218 *
217219 * @param metadata - The metadata associated with the route tree node.
220+ * @param redirectTo - Optional path to redirect to, if specified.
218221 * @param parentInjector - The dependency injection container for the parent route.
219222 * @param invokeGetPrerenderParams - A flag indicating whether to invoke the `getPrerenderParams` function.
220223 * @param includePrerenderFallbackRoutes - A flag indicating whether to include fallback routes in the result.
221224 * @returns An async iterable iterator that yields route tree node metadata for each SSG path or errors.
222225 */
223226async function * handleSSGRoute (
224227 metadata : ServerConfigRouteTreeNodeMetadata ,
228+ redirectTo : string | undefined ,
225229 parentInjector : Injector ,
226230 invokeGetPrerenderParams : boolean ,
227231 includePrerenderFallbackRoutes : boolean ,
@@ -239,6 +243,10 @@ async function* handleSSGRoute(
239243 delete meta [ 'getPrerenderParams' ] ;
240244 }
241245
246+ if ( redirectTo !== undefined ) {
247+ meta . redirectTo = redirectTo ;
248+ }
249+
242250 if ( ! URL_PARAMETER_REGEXP . test ( currentRoutePath ) ) {
243251 // Route has no parameters
244252 yield {
@@ -301,30 +309,6 @@ async function* handleSSGRoute(
301309 }
302310}
303311
304- /**
305- * Resolves the `redirectTo` property for a given route.
306- *
307- * This function processes the `redirectTo` property to ensure that it correctly
308- * resolves relative to the current route path. If `redirectTo` is an absolute path,
309- * it is returned as is. If it is a relative path, it is resolved based on the current route path.
310- *
311- * @param routePath - The current route path.
312- * @param redirectTo - The target path for redirection.
313- * @returns The resolved redirect path as a string.
314- */
315- function resolveRedirectTo ( routePath : string , redirectTo : string ) : string {
316- if ( redirectTo [ 0 ] === '/' ) {
317- // If the redirectTo path is absolute, return it as is.
318- return redirectTo ;
319- }
320-
321- // Resolve relative redirectTo based on the current route path.
322- const segments = routePath . split ( '/' ) ;
323- segments . pop ( ) ; // Remove the last segment to make it relative.
324-
325- return joinUrlParts ( ...segments , redirectTo ) ;
326- }
327-
328312/**
329313 * Builds a server configuration route tree from the given server routes configuration.
330314 *
@@ -456,7 +440,6 @@ export async function getRoutesFromAngularRouterConfig(
456440 includePrerenderFallbackRoutes,
457441 } ) ;
458442
459- let seenAppShellRoute : string | undefined ;
460443 for await ( const result of traverseRoutes ) {
461444 if ( 'error' in result ) {
462445 errors . push ( result . error ) ;
@@ -542,7 +525,7 @@ export async function extractRoutesAndCreateRouteTree(
542525 ) ;
543526
544527 for ( const { route, ...metadata } of routes ) {
545- if ( metadata . redirectTo !== undefined ) {
528+ if ( metadata . redirectTo ?. [ 0 ] === '/' ) {
546529 metadata . redirectTo = joinUrlParts ( baseHref , metadata . redirectTo ) ;
547530 }
548531
0 commit comments