Skip to content

Commit 74b3e2d

Browse files
committed
fix(@angular/ssr): add validation to prevent use of provideServerRoutesConfig in browser context
Introduced an error check to ensure that 'provideServerRoutesConfig' is not utilized in the browser part of the application. This helps avoid unintended behavior. (cherry picked from commit 58a648a)
1 parent c8e1521 commit 74b3e2d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/angular/ssr/src/global.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@
77
*/
88

99
declare const ngDevMode: boolean | undefined;
10+
11+
/**
12+
* Indicates whether the application is operating in server-rendering mode.
13+
*
14+
* `ngServerMode` is a global flag set by Angular's server-side rendering mechanisms,
15+
* typically configured by `provideServerRendering` and `platformServer` during runtime.
16+
*
17+
* @remarks
18+
* - **Internal Angular Flag**: This is an *internal* Angular flag (not a public API), avoid relying on it in application code.
19+
* - **Avoid Direct Use**: This variable is intended for runtime configuration; it should not be accessed directly in application code.
20+
*/
21+
declare const ngServerMode: boolean | undefined;

packages/angular/ssr/src/routes/route-config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ export const SERVER_ROUTES_CONFIG = new InjectionToken<ServerRoute[]>('SERVER_RO
169169
* @developerPreview
170170
*/
171171
export function provideServerRoutesConfig(routes: ServerRoute[]): EnvironmentProviders {
172+
if (typeof ngServerMode === 'undefined' || !ngServerMode) {
173+
throw new Error(
174+
`The 'provideServerRoutesConfig' function should not be invoked within the browser portion of the application.`,
175+
);
176+
}
177+
172178
return makeEnvironmentProviders([
173179
{
174180
provide: SERVER_ROUTES_CONFIG,

0 commit comments

Comments
 (0)