feat(core): introduce BootstrapContext for improved server bootstrapping (#63562)

This commit introduces a number of changes to the server bootstrapping process to make it more robust and less error-prone, especially for concurrent requests.

Previously, the server rendering process relied on a module-level global platform injector. This could lead to issues in server-side rendering environments where multiple requests are processed concurrently, as they could inadvertently share or overwrite the global injector state.

The new approach introduces a `BootstrapContext` that is passed to the `bootstrapApplication` function. This context provides a platform reference that is scoped to the individual request, ensuring that each server-side render has an isolated platform injector. This prevents state leakage between concurrent requests and makes the overall process more reliable.

BREAKING CHANGE:
The server-side bootstrapping process has been changed to eliminate the reliance on a global platform injector.

Before:
```ts
const bootstrap = () => bootstrapApplication(AppComponent, config);
```

After:
```ts
const bootstrap = (context: BootstrapContext) =>
  bootstrapApplication(AppComponent, config, context);
```

A schematic is provided to automatically update `main.server.ts` files to pass the `BootstrapContext` to the `bootstrapApplication` call.

In addition, `getPlatform()` and `destroyPlatform()` will now return `null` and be a no-op respectively when running in a server environment.

PR Close #63562
This commit is contained in:
Alan Agius
2025-09-08 09:27:03 +00:00
committed by Andrew Scott
parent 3dc5056242
commit 28926ba92c
29 changed files with 558 additions and 82 deletions
@@ -5,6 +5,7 @@
```ts
import { ApplicationRef } from '@angular/core';
import { BootstrapContext } from '@angular/platform-browser';
import { EnvironmentProviders } from '@angular/core';
import * as i0 from '@angular/core';
import * as i1 from '@angular/platform-browser';
@@ -27,7 +28,7 @@ export interface PlatformConfig {
url?: string;
}
// @public (undocumented)
// @public
export function platformServer(extraProviders?: StaticProvider[] | undefined): PlatformRef;
// @public
@@ -45,7 +46,7 @@ export class PlatformState {
export function provideServerRendering(): EnvironmentProviders;
// @public
export function renderApplication<T>(bootstrap: () => Promise<ApplicationRef>, options: {
export function renderApplication(bootstrap: (context: BootstrapContext) => Promise<ApplicationRef>, options: {
document?: string | Document;
url?: string;
platformProviders?: Provider[];