Skip to content

Commit

Permalink
refactor(@angular/ssr): skip navigating to the root page during route…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
alan-agius4 committed Dec 9, 2024
1 parent e126bf9 commit 5e2f18e
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
*/

import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
import { ApplicationRef, Compiler, Injector, runInInjectionContext, ɵConsole } from '@angular/core';
import {
APP_INITIALIZER,
ApplicationRef,
Compiler,
Injector,
inject,
runInInjectionContext,
ɵConsole,
} from '@angular/core';
import { INITIAL_CONFIG, platformServer } from '@angular/platform-server';
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
import { ROUTES, Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
import { ServerAssets } from '../assets';
import { Console } from '../console';
import { AngularAppManifest, getAngularAppManifest } from '../manifest';
Expand All @@ -25,6 +33,12 @@ import {
} from './route-config';
import { RouteTree, RouteTreeNodeMetadata } from './route-tree';

/**
* Constant representing the path for route extraction.
* This path is used during the route extraction process and does not affect the application's actual routing behavior.
*/
const EXTRACT_ROUTES_PATH = 'ng-routes-internal';

/**
* Regular expression to match segments preceded by a colon in a string.
*/
Expand Down Expand Up @@ -407,12 +421,26 @@ export async function getRoutesFromAngularRouterConfig(
const platformRef = platformServer([
{
provide: INITIAL_CONFIG,
useValue: { document, url: `${protocol}//${host}/` },
// The route below is intentionally configured with a non-existent path
// to prevent the root route from rendering during the route extraction process.
useValue: { document, url: `${protocol}//${host}/${EXTRACT_ROUTES_PATH}` },
},
{
provide: ɵConsole,
useFactory: () => new Console(),
},
{
multi: true,
provide: APP_INITIALIZER,
useFactory: () => {
// Adds an 'internal' route to extract routes.
// This route doesn't perform any action but ensures the use of a non-existent path to avoid route
// mismatch errors in Angular router.
return () => {
inject(ROUTES).unshift([{ path: EXTRACT_ROUTES_PATH, children: [] }]);
};
},
},
]);

try {
Expand All @@ -427,9 +455,15 @@ export async function getRoutesFromAngularRouterConfig(

// Wait until the application is stable.
await applicationRef.whenStable();

const injector = applicationRef.injector;
const router = injector.get(Router);

// Remove the 'internal' route as it is no longer needed.
if (router.config[0].path === EXTRACT_ROUTES_PATH) {
injector.get(ROUTES).shift();
router.config.shift();
}

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

Expand Down

0 comments on commit 5e2f18e

Please sign in to comment.