From fb05e7f0abd9d68ac03f243c7774260619b8a623 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Fri, 15 Nov 2024 17:05:45 -0800 Subject: [PATCH] fix(@angular/ssr): use wildcard server route configuration on the '/' route when the app router is empty (cherry picked from commit d3dd8f00d368e77772476ec776548e11d8e0b4b6) --- packages/angular/ssr/src/routes/ng-routes.ts | 9 +++++++-- packages/angular/ssr/test/routes/ng-routes_spec.ts | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/angular/ssr/src/routes/ng-routes.ts b/packages/angular/ssr/src/routes/ng-routes.ts index 2a54bbd659f6..5bba8b7d9aba 100644 --- a/packages/angular/ssr/src/routes/ng-routes.ts +++ b/packages/angular/ssr/src/routes/ng-routes.ts @@ -480,11 +480,16 @@ export async function getRoutesFromAngularRouterConfig( } } } else { - const renderMode = serverConfigRouteTree?.match('')?.renderMode ?? RenderMode.Prerender; + const rootRouteMetadata = serverConfigRouteTree?.match('') ?? { + route: '', + renderMode: RenderMode.Prerender, + }; routesResults.push({ + ...rootRouteMetadata, + // Matched route might be `/*` or `/**`, which would make Angular serve all routes rather than just `/`. + // So we limit to just `/` for the empty app router case. route: '', - renderMode, }); } diff --git a/packages/angular/ssr/test/routes/ng-routes_spec.ts b/packages/angular/ssr/test/routes/ng-routes_spec.ts index 77e3a910bec0..4b334c25eac9 100644 --- a/packages/angular/ssr/test/routes/ng-routes_spec.ts +++ b/packages/angular/ssr/test/routes/ng-routes_spec.ts @@ -340,8 +340,8 @@ describe('extractRoutesAndCreateRouteTree', () => { ); }); - it('should apply RenderMode matching the wildcard when no Angular routes are defined', async () => { - setAngularAppTestingManifest([], [{ path: '**', renderMode: RenderMode.Server }]); + it('should use wildcard configuration when no Angular routes are defined', async () => { + setAngularAppTestingManifest([], [{ path: '**', renderMode: RenderMode.Server, status: 201 }]); const { errors, routeTree } = await extractRoutesAndCreateRouteTree( url, @@ -351,6 +351,8 @@ describe('extractRoutesAndCreateRouteTree', () => { ); expect(errors).toHaveSize(0); - expect(routeTree.toObject()).toEqual([{ route: '/', renderMode: RenderMode.Server }]); + expect(routeTree.toObject()).toEqual([ + { route: '/', renderMode: RenderMode.Server, status: 201 }, + ]); }); });