Skip to content

Commit

Permalink
fix(@angular/ssr): prioritize the first matching route over subsequen…
Browse files Browse the repository at this point in the history
…t ones

Ensures that the SSR router gives precedence to the first matching route, addressing the issue where later conflicting routes.

This change prevents the incorrect prioritization of routes and ensures the intended route is matched first, aligning routing behavior.

Closes: #29539
(cherry picked from commit 6448f80)
  • Loading branch information
alan-agius4 authored and jkrems committed Feb 3, 2025
1 parent 2dec1e7 commit 5bf5e5f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ export async function getRoutesFromAngularRouterConfig(
// Wait until the application is stable.
await applicationRef.whenStable();

const routesResults: RouteTreeNodeMetadata[] = [];
const errors: string[] = [];

let baseHref =
Expand All @@ -581,11 +580,12 @@ export async function getRoutesFromAngularRouterConfig(
if (errors.length) {
return {
baseHref,
routes: routesResults,
routes: [],
errors,
};
}

const routesResults: RouteTreeNodeMetadata[] = [];
if (router.config.length) {
// Retrieve all routes from the Angular router configuration.
const traverseRoutes = traverseRoutesConfig({
Expand All @@ -599,11 +599,19 @@ export async function getRoutesFromAngularRouterConfig(
entryPointToBrowserMapping,
});

for await (const result of traverseRoutes) {
if ('error' in result) {
errors.push(result.error);
} else {
routesResults.push(result);
const seenRoutes: Set<string> = new Set();
for await (const routeMetadata of traverseRoutes) {
if ('error' in routeMetadata) {
errors.push(routeMetadata.error);
continue;
}

// If a result already exists for the exact same route, subsequent matches should be ignored.
// This aligns with Angular's app router behavior, which prioritizes the first route.
const routePath = routeMetadata.route;
if (!seenRoutes.has(routePath)) {
routesResults.push(routeMetadata);
seenRoutes.add(routePath);
}
}

Expand Down
34 changes: 34 additions & 0 deletions packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,38 @@ describe('extractRoutesAndCreateRouteTree', () => {
expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toHaveSize(2);
});

it('should give precedence to the first matching route over subsequent ones', async () => {
setAngularAppTestingManifest(
[
{
path: '',
children: [
{ path: 'home', component: DummyComponent },
{ path: '**', component: DummyComponent },
],
},
// The following routes should be ignored due to Angular's routing behavior:
// - ['', '**'] and ['**'] are equivalent, and the first match takes precedence.
// - ['', 'home'] and ['home'] are equivalent, and the first match takes precedence.
{
path: 'home',
redirectTo: 'never',
},
{
path: '**',
redirectTo: 'never',
},
],
[{ path: '**', renderMode: RenderMode.Server }],
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url });
expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toEqual([
{ route: '/', renderMode: RenderMode.Server },
{ route: '/home', renderMode: RenderMode.Server },
{ route: '/**', renderMode: RenderMode.Server },
]);
});
});

0 comments on commit 5bf5e5f

Please sign in to comment.