Skip to content

Commit

Permalink
fix(@angular/ssr): add validation to prevent use of `provideServerRou…
Browse files Browse the repository at this point in the history
…tesConfig` 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.
  • Loading branch information
alan-agius4 committed Nov 5, 2024
1 parent e6ff801 commit 58a648a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/angular/ssr/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions packages/angular/ssr/src/routes/route-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ export const SERVER_ROUTES_CONFIG = new InjectionToken<ServerRoute[]>('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,
Expand Down

0 comments on commit 58a648a

Please sign in to comment.