Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions goldens/public-api/angular/ssr/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { DefaultExport } from '@angular/router';
import { EnvironmentProviders } from '@angular/core';
import { InjectionToken } from '@angular/core';
import { Provider } from '@angular/core';
import { Type } from '@angular/core';

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

// @public
export const IS_DISCOVERING_ROUTES: InjectionToken<boolean>;

// @public
export enum PrerenderFallback {
Client = 1,
Expand Down
2 changes: 2 additions & 0 deletions packages/angular/ssr/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export {
type ServerRouteServer,
type ServerRouteCommon,
} from './src/routes/route-config';

export { IS_DISCOVERING_ROUTES } from './src/routes/ng-routes';
22 changes: 22 additions & 0 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ApplicationRef,
Compiler,
EnvironmentInjector,
InjectionToken,
Injector,
createEnvironmentInjector,
runInInjectionContext,
Expand All @@ -23,6 +24,7 @@ import {
Router,
ɵloadChildren as loadChildrenHelper,
} from '@angular/router';

import { ServerAssets } from '../assets';
import { Console } from '../console';
import { AngularAppManifest, getAngularAppManifest } from '../manifest';
Expand All @@ -39,6 +41,22 @@ import {
} from './route-config';
import { RouteTree, RouteTreeNodeMetadata } from './route-tree';

/**
* A DI token that indicates whether the application is in the process of discovering routes.
*
* This token is provided with the value `true` when route discovery is active, allowing other
* parts of the application to conditionally execute logic. For example, it can be used to
* disable features or behaviors that are not necessary or might interfere with the route
* discovery process.
*/
export const IS_DISCOVERING_ROUTES = new InjectionToken<boolean>(
typeof ngDevMode === 'undefined' || ngDevMode ? 'IS_DISCOVERING_ROUTES' : '',
{
providedIn: 'platform',
factory: () => false,
},
);

interface Route extends AngularRoute {
ɵentryName?: string;
}
Expand Down Expand Up @@ -623,6 +641,10 @@ export async function getRoutesFromAngularRouterConfig(
provide: ɵENABLE_ROOT_COMPONENT_BOOTSTRAP,
useValue: false,
},
{
provide: IS_DISCOVERING_ROUTES,
useValue: true,
},
]);

try {
Expand Down
24 changes: 23 additions & 1 deletion packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
provideRouter,
withEnabledBlockingInitialNavigation,
} from '@angular/router';
import { extractRoutesAndCreateRouteTree } from '../../src/routes/ng-routes';
import { IS_DISCOVERING_ROUTES, extractRoutesAndCreateRouteTree } from '../../src/routes/ng-routes';
import { PrerenderFallback, RenderMode } from '../../src/routes/route-config';
import { setAngularAppTestingManifest } from '../testing-utils';

Expand Down Expand Up @@ -790,4 +790,26 @@ describe('extractRoutesAndCreateRouteTree', () => {
{ route: '/home', renderMode: RenderMode.Server },
]);
});

it('should provide `IS_DISCOVERING_ROUTES` as `true` during route discovery', async () => {
let isDiscoveringRoutes: boolean | undefined;

setAngularAppTestingManifest(
[
{
path: 'lazy',
loadChildren: () => {
isDiscoveringRoutes = inject(IS_DISCOVERING_ROUTES);

return [];
},
},
],
[{ path: '**', renderMode: RenderMode.Server }],
);

await extractRoutesAndCreateRouteTree({ url });

expect(isDiscoveringRoutes).toBeTrue();
});
});