Skip to content

Commit dee3717

Browse files
committed
fix(@angular/ssr): introduce DI token to signal route discovery process
A new DI token, `IS_DISCOVERING_ROUTES`, is introduced to provide a clear signal for when the application is operating in route discovery mode. This token is provided with the value `true` within the route extraction providers. Other services and components can inject this token to conditionally alter their behavior, for instance, to disable functionality that is not required or could interfere with the route discovery process. Closes angular#32474 (cherry picked from commit 0f41585)
1 parent 3627063 commit dee3717

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

goldens/public-api/angular/ssr/index.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { DefaultExport } from '@angular/router';
88
import { EnvironmentProviders } from '@angular/core';
9+
import { InjectionToken } from '@angular/core';
910
import { Provider } from '@angular/core';
1011
import { Type } from '@angular/core';
1112

@@ -26,6 +27,9 @@ export interface AngularAppEngineOptions {
2627
// @public
2728
export function createRequestHandler(handler: RequestHandlerFunction): RequestHandlerFunction;
2829

30+
// @public
31+
export const IS_DISCOVERING_ROUTES: InjectionToken<boolean>;
32+
2933
// @public
3034
export enum PrerenderFallback {
3135
Client = 1,

packages/angular/ssr/public_api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ export {
2424
type ServerRouteServer,
2525
type ServerRouteCommon,
2626
} from './src/routes/route-config';
27+
28+
export { IS_DISCOVERING_ROUTES } from './src/routes/ng-routes';

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ApplicationRef,
1212
Compiler,
1313
EnvironmentInjector,
14+
InjectionToken,
1415
Injector,
1516
createEnvironmentInjector,
1617
runInInjectionContext,
@@ -23,6 +24,7 @@ import {
2324
Router,
2425
ɵloadChildren as loadChildrenHelper,
2526
} from '@angular/router';
27+
2628
import { ServerAssets } from '../assets';
2729
import { Console } from '../console';
2830
import { AngularAppManifest, getAngularAppManifest } from '../manifest';
@@ -39,6 +41,22 @@ import {
3941
} from './route-config';
4042
import { RouteTree, RouteTreeNodeMetadata } from './route-tree';
4143

44+
/**
45+
* A DI token that indicates whether the application is in the process of discovering routes.
46+
*
47+
* This token is provided with the value `true` when route discovery is active, allowing other
48+
* parts of the application to conditionally execute logic. For example, it can be used to
49+
* disable features or behaviors that are not necessary or might interfere with the route
50+
* discovery process.
51+
*/
52+
export const IS_DISCOVERING_ROUTES = new InjectionToken<boolean>(
53+
typeof ngDevMode === 'undefined' || ngDevMode ? 'IS_DISCOVERING_ROUTES' : '',
54+
{
55+
providedIn: 'platform',
56+
factory: () => false,
57+
},
58+
);
59+
4260
interface Route extends AngularRoute {
4361
ɵentryName?: string;
4462
}
@@ -623,6 +641,10 @@ export async function getRoutesFromAngularRouterConfig(
623641
provide: ɵENABLE_ROOT_COMPONENT_BOOTSTRAP,
624642
useValue: false,
625643
},
644+
{
645+
provide: IS_DISCOVERING_ROUTES,
646+
useValue: true,
647+
},
626648
]);
627649

628650
try {

packages/angular/ssr/test/routes/ng-routes_spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
provideRouter,
1919
withEnabledBlockingInitialNavigation,
2020
} from '@angular/router';
21-
import { extractRoutesAndCreateRouteTree } from '../../src/routes/ng-routes';
21+
import { IS_DISCOVERING_ROUTES, extractRoutesAndCreateRouteTree } from '../../src/routes/ng-routes';
2222
import { PrerenderFallback, RenderMode } from '../../src/routes/route-config';
2323
import { setAngularAppTestingManifest } from '../testing-utils';
2424

@@ -790,4 +790,26 @@ describe('extractRoutesAndCreateRouteTree', () => {
790790
{ route: '/home', renderMode: RenderMode.Server },
791791
]);
792792
});
793+
794+
it('should provide `IS_DISCOVERING_ROUTES` as `true` during route discovery', async () => {
795+
let isDiscoveringRoutes: boolean | undefined;
796+
797+
setAngularAppTestingManifest(
798+
[
799+
{
800+
path: 'lazy',
801+
loadChildren: () => {
802+
isDiscoveringRoutes = inject(IS_DISCOVERING_ROUTES);
803+
804+
return [];
805+
},
806+
},
807+
],
808+
[{ path: '**', renderMode: RenderMode.Server }],
809+
);
810+
811+
await extractRoutesAndCreateRouteTree({ url });
812+
813+
expect(isDiscoveringRoutes).toBeTrue();
814+
});
793815
});

0 commit comments

Comments
 (0)