From 74b3e2d51c6cf605abd05da81dc7b4c3ccd9b3ea Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 5 Nov 2024 09:55:09 +0000 Subject: [PATCH] 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 58a648aadc0e6d9b1ff2107df0f67ca60eae7ef6) --- packages/angular/ssr/src/global.d.ts | 12 ++++++++++++ packages/angular/ssr/src/routes/route-config.ts | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/packages/angular/ssr/src/global.d.ts b/packages/angular/ssr/src/global.d.ts index 3465e8098860..a1aa4ddd4684 100644 --- a/packages/angular/ssr/src/global.d.ts +++ b/packages/angular/ssr/src/global.d.ts @@ -7,3 +7,15 @@ */ declare const ngDevMode: boolean | undefined; + +/** + * Indicates whether the application is operating in server-rendering mode. + * + * `ngServerMode` is a global flag set by Angular's server-side rendering mechanisms, + * typically configured by `provideServerRendering` and `platformServer` during runtime. + * + * @remarks + * - **Internal Angular Flag**: This is an *internal* Angular flag (not a public API), avoid relying on it in application code. + * - **Avoid Direct Use**: This variable is intended for runtime configuration; it should not be accessed directly in application code. + */ +declare const ngServerMode: boolean | undefined; diff --git a/packages/angular/ssr/src/routes/route-config.ts b/packages/angular/ssr/src/routes/route-config.ts index ac6fd6203bca..e9271f5e111b 100644 --- a/packages/angular/ssr/src/routes/route-config.ts +++ b/packages/angular/ssr/src/routes/route-config.ts @@ -169,6 +169,12 @@ export const SERVER_ROUTES_CONFIG = new InjectionToken('SERVER_RO * @developerPreview */ export function provideServerRoutesConfig(routes: ServerRoute[]): EnvironmentProviders { + if (typeof ngServerMode === 'undefined' || !ngServerMode) { + throw new Error( + `The 'provideServerRoutesConfig' function should not be invoked within the browser portion of the application.`, + ); + } + return makeEnvironmentProviders([ { provide: SERVER_ROUTES_CONFIG,