Skip to content

Commit 7661712

Browse files
committed
fixup! fix(@angular/ssr): resolve route parameters in redirect targets
1 parent 1281fa8 commit 7661712

1 file changed

Lines changed: 40 additions & 16 deletions

File tree

‎packages/angular/ssr/src/routes/ng-routes.ts‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,14 @@ async function* handleRoute(options: {
203203
`Please use one of the following redirect response codes: ${[...VALID_REDIRECT_RESPONSE_CODES.values()].join(', ')}.`,
204204
};
205205
} else if (typeof redirectTo === 'string') {
206-
// Reference the route parameters by their position in the URL (see `buildPathWithParams`).
207-
const routeSegments = joinUrlParts(baseHref, currentRoutePath).split('/');
208206
parentRoutes.set(metadata.route, {
209207
...metadata,
210-
redirectTo: resolveRedirectTo(
208+
redirectTo: referenceRedirectParams(
209+
route,
210+
redirectTo,
211211
parentRoute,
212-
replaceRedirectParams(
213-
route,
214-
redirectTo,
215-
(parameter) => `*${routeSegments.lastIndexOf(parameter)}`,
216-
),
212+
baseHref,
213+
currentRoutePath,
217214
),
218215
relativeRedirect: redirectTo[0] !== '/' ? true : undefined,
219216
});
@@ -468,15 +465,12 @@ async function* handleSSGRoute(
468465

469466
const redirectTo = typeof route.redirectTo === 'string' ? route.redirectTo : undefined;
470467
if (redirectTo !== undefined) {
471-
// Reference the route parameters by their position in the URL (see `buildPathWithParams`).
472-
const routeSegments = joinUrlParts(baseHref, currentRoutePath).split('/');
473-
meta.redirectTo = resolveRedirectTo(
468+
meta.redirectTo = referenceRedirectParams(
469+
route,
470+
redirectTo,
474471
parentRoute,
475-
replaceRedirectParams(
476-
route,
477-
redirectTo,
478-
(parameter) => `*${routeSegments.lastIndexOf(parameter)}`,
479-
),
472+
baseHref,
473+
currentRoutePath,
480474
);
481475
meta.relativeRedirect = redirectTo[0] !== '/' ? true : undefined;
482476
}
@@ -660,6 +654,36 @@ function replaceRedirectParams(
660654
return resolvedPath + redirectTo.slice(path.length);
661655
}
662656

657+
/**
658+
* Resolves the `redirectTo` property of a route (see `resolveRedirectTo`), and references the
659+
* route parameters, such as `:id`, by their position in the URL (see `buildPathWithParams`).
660+
*
661+
* @param route - The redirecting route.
662+
* @param redirectTo - The target path for redirection.
663+
* @param parentRoute - The path of the parent route.
664+
* @param baseHref - The base href of the application.
665+
* @param currentRoutePath - The path of the redirecting route.
666+
* @returns The resolved redirect path with the route parameters referenced by position.
667+
*/
668+
function referenceRedirectParams(
669+
route: Route,
670+
redirectTo: string,
671+
parentRoute: string,
672+
baseHref: string,
673+
currentRoutePath: string,
674+
): string {
675+
const routeSegments = joinUrlParts(baseHref, currentRoutePath).split('/');
676+
677+
return resolveRedirectTo(
678+
parentRoute,
679+
replaceRedirectParams(
680+
route,
681+
redirectTo,
682+
(parameter) => `*${routeSegments.lastIndexOf(parameter)}`,
683+
),
684+
);
685+
}
686+
663687
/**
664688
* Builds a server configuration route tree from the given server routes configuration.
665689
*

0 commit comments

Comments
 (0)