Skip to content

Commit 29297ec

Browse files
committed
fix(@angular/ssr): replace all route parameters when resolving relative redirects
When resolving a relative `redirectTo` property for a route with multiple path parameters (e.g. `:param1/:param2`), only the first parameter segment was being converted to `*` because a non-global regular expression was used. This commit updates `resolveRedirectTo` to use `URL_PARAMETER_GLOBAL_REGEXP` so that all parameter placeholders in the route path are properly replaced with `*`. Fixes angular#33504
1 parent 1b78bbd commit 29297ec

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ function resolveRedirectTo(routePath: string, redirectTo: string): string {
547547
}
548548

549549
// Resolve relative redirectTo based on the current route path.
550-
const segments = routePath.replace(URL_PARAMETER_REGEXP, '*').split('/');
550+
const segments = routePath.replace(URL_PARAMETER_GLOBAL_REGEXP, '*').split('/');
551551
segments.pop(); // Remove the last segment to make it relative.
552552

553553
return joinUrlParts(...segments, redirectTo);

packages/angular/ssr/test/routes/ng-routes_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,35 @@ describe('extractRoutesAndCreateRouteTree', () => {
502502
]);
503503
});
504504

505+
it('should extract nested redirects with multiple path parameters', async () => {
506+
setAngularAppTestingManifest(
507+
[
508+
{
509+
path: ':param1/:param2',
510+
children: [
511+
{
512+
path: '',
513+
pathMatch: 'full',
514+
redirectTo: 'thing',
515+
},
516+
{
517+
path: 'thing',
518+
component: DummyComponent,
519+
},
520+
],
521+
},
522+
],
523+
[{ path: '**', renderMode: RenderMode.Server }],
524+
);
525+
526+
const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url });
527+
expect(errors).toHaveSize(0);
528+
expect(routeTree.toObject()).toEqual([
529+
{ route: '/*/*', renderMode: RenderMode.Server, redirectTo: '/*/*/thing' },
530+
{ route: '/*/*/thing', renderMode: RenderMode.Server },
531+
]);
532+
});
533+
505534
it('should not resolve parameterized routes for SSG when `invokeGetPrerenderParams` is false', async () => {
506535
setAngularAppTestingManifest(
507536
[

0 commit comments

Comments
 (0)