Skip to content

Commit 6e1e4c5

Browse files
committed
refactor(@angular/ssr): skip rendering root page during route extraction
To prevent rendering the root route while extracting routes, a non-existent path (`/ng-extract-routes`) is intentionally used. This ensures unnecessary rendering during the route extraction process where now only the app component is rendered.
1 parent e126bf9 commit 6e1e4c5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
1010
import { ApplicationRef, Compiler, Injector, runInInjectionContext, ɵConsole } from '@angular/core';
1111
import { INITIAL_CONFIG, platformServer } from '@angular/platform-server';
12-
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
12+
import { ROUTES, Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
1313
import { ServerAssets } from '../assets';
1414
import { Console } from '../console';
1515
import { AngularAppManifest, getAngularAppManifest } from '../manifest';
@@ -25,6 +25,12 @@ import {
2525
} from './route-config';
2626
import { RouteTree, RouteTreeNodeMetadata } from './route-tree';
2727

28+
/**
29+
* Constant representing the path for route extraction.
30+
* This path is used during the route extraction process and does not affect the application's actual routing behavior.
31+
*/
32+
const EXTRACT_ROUTES_PATH = 'ng-routes-internal';
33+
2834
/**
2935
* Regular expression to match segments preceded by a colon in a string.
3036
*/
@@ -407,7 +413,16 @@ export async function getRoutesFromAngularRouterConfig(
407413
const platformRef = platformServer([
408414
{
409415
provide: INITIAL_CONFIG,
410-
useValue: { document, url: `${protocol}//${host}/` },
416+
// The route below is intentionally configured with a non-existent path
417+
// to prevent the root route from rendering during the route extraction process.
418+
useValue: { document, url: `${protocol}//${host}/${EXTRACT_ROUTES_PATH}` },
419+
},
420+
{
421+
provide: ROUTES,
422+
multi: true,
423+
// Adds an 'internal' route to extract routes.
424+
// This route doesn't perform any action but ensures the use of a non-existent path to avoid route mismatch errors in Angular router.
425+
useValue: [EXTRACT_ROUTES_PATH],
411426
},
412427
{
413428
provide: ɵConsole,

0 commit comments

Comments
 (0)