Skip to content

Commit 7150c4c

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

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
*/
88

99
import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
10-
import { ApplicationRef, Compiler, Injector, runInInjectionContext, ɵConsole } from '@angular/core';
10+
import {
11+
APP_INITIALIZER,
12+
ApplicationRef,
13+
Compiler,
14+
Injector,
15+
inject,
16+
runInInjectionContext,
17+
ɵConsole,
18+
} from '@angular/core';
1119
import { INITIAL_CONFIG, platformServer } from '@angular/platform-server';
12-
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
20+
import { ROUTES, Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
1321
import { ServerAssets } from '../assets';
1422
import { Console } from '../console';
1523
import { AngularAppManifest, getAngularAppManifest } from '../manifest';
@@ -25,6 +33,12 @@ import {
2533
} from './route-config';
2634
import { RouteTree, RouteTreeNodeMetadata } from './route-tree';
2735

36+
/**
37+
* Constant representing the path for route extraction.
38+
* This path is used during the route extraction process and does not affect the application's actual routing behavior.
39+
*/
40+
const EXTRACT_ROUTES_PATH = 'ng-routes-internal';
41+
2842
/**
2943
* Regular expression to match segments preceded by a colon in a string.
3044
*/
@@ -407,12 +421,26 @@ export async function getRoutesFromAngularRouterConfig(
407421
const platformRef = platformServer([
408422
{
409423
provide: INITIAL_CONFIG,
410-
useValue: { document, url: `${protocol}//${host}/` },
424+
// The route below is intentionally configured with a non-existent path
425+
// to prevent the root route from rendering during the route extraction process.
426+
useValue: { document, url: `${protocol}//${host}/${EXTRACT_ROUTES_PATH}` },
411427
},
412428
{
413429
provide: ɵConsole,
414430
useFactory: () => new Console(),
415431
},
432+
{
433+
multi: true,
434+
provide: APP_INITIALIZER,
435+
useFactory: () => {
436+
// Adds an 'internal' route to extract routes.
437+
// This route doesn't perform any action but ensures the use of a non-existent path to avoid route
438+
// mismatch errors in Angular router.
439+
return () => {
440+
inject(ROUTES).unshift([{ path: EXTRACT_ROUTES_PATH, children: [] }]);
441+
};
442+
},
443+
},
416444
]);
417445

418446
try {
@@ -427,9 +455,15 @@ export async function getRoutesFromAngularRouterConfig(
427455

428456
// Wait until the application is stable.
429457
await applicationRef.whenStable();
430-
431458
const injector = applicationRef.injector;
432459
const router = injector.get(Router);
460+
461+
// Remove the 'internal' route as it is no longer needed.
462+
if (router.config[0].path === EXTRACT_ROUTES_PATH) {
463+
injector.get(ROUTES).shift();
464+
router.config.shift();
465+
}
466+
433467
const routesResults: RouteTreeNodeMetadata[] = [];
434468
const errors: string[] = [];
435469

0 commit comments

Comments
 (0)