angular/modules/ssr-benchmarks/src/main.server.ts
Alan Agius 28926ba92c 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
2025-09-09 10:57:09 -07:00

44 lines
1.5 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {ɵenableProfiling} from '@angular/core';
import {bootstrapApplication, BootstrapContext} from '@angular/platform-browser';
import {AppComponent} from './app/app.component';
import {config} from './app/app.config.server';
import {renderApplication, ɵENABLE_DOM_EMULATION} from '@angular/platform-server';
const bootstrap = (context: BootstrapContext) =>
bootstrapApplication(AppComponent, config, context);
/**
* Function that will profile the server-side rendering
*
* @param DISABLE_DOM_EMULATION will prevent the platform-server from using the DominoAdapter, `false` by default
* (But won't prevent the monkey patching of DOM apis as this is introduced by the CLI)
*/
const render = (DISABLE_DOM_EMULATION: boolean = false) => {
ɵenableProfiling();
let doc: Document | string;
if (DISABLE_DOM_EMULATION) {
doc = document.implementation.createHTMLDocument('');
doc.body.innerHTML = '<app-root></app-root>';
} else {
doc = '<html><head></head><body><app-root></app-root></body></html>';
}
return renderApplication(bootstrap, {
document: doc,
platformProviders: [{provide: ɵENABLE_DOM_EMULATION, useValue: !DISABLE_DOM_EMULATION}],
});
};
export {render};
// Tooling expects a default export but we don't use/need it.
export default bootstrap;