From a995c8ea6d17778af031c2f9797e52739ea4dc81 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 24 Sep 2024 07:41:10 +0000 Subject: [PATCH] fix(@angular/build): prevent prerendering of catch-all routes Updated the build process to exclude catch-all routes from being prerendered. --- .../build/src/utils/server-rendering/prerender.ts | 7 ++++++- .../server-rendering/server-routes-output-mode-static.ts | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/angular/build/src/utils/server-rendering/prerender.ts b/packages/angular/build/src/utils/server-rendering/prerender.ts index dd26395e4229..2dae25cff0cc 100644 --- a/packages/angular/build/src/utils/server-rendering/prerender.ts +++ b/packages/angular/build/src/utils/server-rendering/prerender.ts @@ -121,6 +121,12 @@ export async function prerenderPages( const serializableRouteTreeNodeForPrerender: WritableSerializableRouteTreeNode = []; for (const metadata of serializableRouteTreeNode) { if (outputMode !== OutputMode.Static && metadata.redirectTo) { + // Skip redirects if output mode is not static. + continue; + } + + if (metadata.route.includes('*')) { + // Skip catch all routes from prerender. continue; } @@ -129,7 +135,6 @@ export async function prerenderPages( case RouteRenderMode.Prerender: case RouteRenderMode.AppShell: serializableRouteTreeNodeForPrerender.push(metadata); - break; case RouteRenderMode.Server: if (outputMode === OutputMode.Static) { diff --git a/tests/legacy-cli/e2e/tests/server-rendering/server-routes-output-mode-static.ts b/tests/legacy-cli/e2e/tests/server-rendering/server-routes-output-mode-static.ts index 16504591fc14..8b8f77869aa0 100644 --- a/tests/legacy-cli/e2e/tests/server-rendering/server-routes-output-mode-static.ts +++ b/tests/legacy-cli/e2e/tests/server-rendering/server-routes-output-mode-static.ts @@ -1,5 +1,5 @@ import { join } from 'node:path'; -import { expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs'; +import { expectFileNotToExist, expectFileToMatch, replaceInFile, writeFile } from '../../utils/fs'; import { noSilentNg, silentNg } from '../../utils/process'; import { setupProjectWithSSRAppEngine } from './setup'; import { existsSync } from 'node:fs'; @@ -36,6 +36,10 @@ export default async function () { path: 'ssg/:id', component: SsgWithParamsComponent, }, + { + path: '**', + component: HomeComponent, + }, ]; `, ); @@ -100,4 +104,7 @@ export default async function () { !existsSync('dist/test-project/server'), 'Server directory should not exist when output-mode is static', ); + + // Should not prerender the catch all + await expectFileNotToExist(join('dist/test-project/browser/**/index.html')); }