Skip to content

Commit 779a0bd

Browse files
committed
fix(@angular/ssr): adopt bootstrapServerApplication for streamlined server bootstrap
This commit updates the SSR implementation to use the new `bootstrapServerApplication` function. This simplifies the server-side rendering (SSR) bootstrap process for standalone applications by making it more explicit and removing the need for a custom `main.server.ts` file that wraps `bootstrapApplication`. Key changes: - The SSR engine is updated to leverage the new bootstrap function. - The `ng add @angular/ssr` schematic is updated to generate code using `bootstrapServerApplication`. - Internal utilities and tests are updated to align with this new approach.
1 parent 7b0f697 commit 779a0bd

File tree

8 files changed

+22
-25
lines changed

8 files changed

+22
-25
lines changed

packages/angular/ssr/src/routes/ng-routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ export async function getRoutesFromAngularRouterConfig(
634634
const moduleRef = await platformRef.bootstrapModule(bootstrap);
635635
applicationRef = moduleRef.injector.get(ApplicationRef);
636636
} else {
637-
applicationRef = await bootstrap();
637+
applicationRef = await bootstrap(platformRef.injector);
638638
}
639639

640640
const injector = applicationRef.injector;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,16 @@ export function withAppShell(
353353
*
354354
* @example
355355
* Basic example of how you can enable server-side rendering in your application
356-
* when using the `bootstrapApplication` function:
356+
* when using the `bootstrapServerApplication` function:
357357
*
358358
* ```ts
359-
* import { bootstrapApplication } from '@angular/platform-browser';
359+
* import { bootstrapServerApplication } from '@angular/platform-server';
360360
* import { provideServerRendering, withRoutes, withAppShell } from '@angular/ssr';
361361
* import { AppComponent } from './app/app.component';
362362
* import { SERVER_ROUTES } from './app/app.server.routes';
363363
* import { AppShellComponent } from './app/app-shell.component';
364364
*
365-
* bootstrapApplication(AppComponent, {
365+
* bootstrapServerApplication(AppComponent, {
366366
* providers: [
367367
* provideServerRendering(
368368
* withRoutes(SERVER_ROUTES),

packages/angular/ssr/src/utils/ng.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import {
1818
INITIAL_CONFIG,
1919
ɵSERVER_CONTEXT as SERVER_CONTEXT,
20+
bootstrapServerApplication,
2021
platformServer,
2122
ɵrenderInternal as renderInternal,
2223
} from '@angular/platform-server';
@@ -31,7 +32,7 @@ import { joinUrlParts, stripIndexHtmlFromURL } from './url';
3132
* - A reference to an Angular component or module (`Type<unknown>`) that serves as the root of the application.
3233
* - A function that returns a `Promise<ApplicationRef>`, which resolves with the root application reference.
3334
*/
34-
export type AngularBootstrap = Type<unknown> | (() => Promise<ApplicationRef>);
35+
export type AngularBootstrap = Type<unknown> | ReturnType<typeof bootstrapServerApplication>;
3536

3637
/**
3738
* Renders an Angular application or module to an HTML string.
@@ -90,7 +91,7 @@ export async function renderAngular(
9091
const moduleRef = await platformRef.bootstrapModule(bootstrap);
9192
applicationRef = moduleRef.injector.get(ApplicationRef);
9293
} else {
93-
applicationRef = await bootstrap();
94+
applicationRef = await bootstrap(platformRef.injector);
9495
}
9596

9697
// Block until application is stable.

packages/angular/ssr/test/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ts_project(
1212
"//:node_modules/@angular/common",
1313
"//:node_modules/@angular/compiler",
1414
"//:node_modules/@angular/core",
15-
"//:node_modules/@angular/platform-browser",
15+
"//:node_modules/@angular/platform-server",
1616
"//:node_modules/@angular/router",
1717
"//:node_modules/@types/node",
1818
"//packages/angular/ssr",

packages/angular/ssr/test/testing-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
Type,
1414
provideZonelessChangeDetection,
1515
} from '@angular/core';
16-
import { bootstrapApplication } from '@angular/platform-browser';
16+
import { bootstrapServerApplication } from '@angular/platform-server';
1717
import { RouterOutlet, Routes, provideRouter } from '@angular/router';
1818
import { destroyAngularServerApp } from '../src/app';
1919
import { ServerAsset, setAngularAppManifest } from '../src/manifest';
@@ -90,8 +90,8 @@ export function setAngularAppTestingManifest(
9090
`,
9191
},
9292
},
93-
bootstrap: async () => () => {
94-
return bootstrapApplication(rootComponent, {
93+
bootstrap: async () => {
94+
return bootstrapServerApplication(rootComponent, {
9595
providers: [
9696
provideZonelessChangeDetection(),
9797
provideRouter(routes),
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { bootstrapApplication } from '@angular/platform-browser';
1+
import { bootstrapServerApplication } from '@angular/platform-server';
22
import { <%= appComponentName %> } from '<%= appComponentPath %>';
33
import { config } from './app/app.config.server';
44

5-
const bootstrap = () => bootstrapApplication(<%= appComponentName %>, config);
6-
7-
export default bootstrap;
5+
export default bootstrapServerApplication(<%= appComponentName %>, config);
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { bootstrapApplication } from '@angular/platform-browser';
1+
import { bootstrapServerApplication } from '@angular/platform-server';
22
import { <%= appComponentName %> } from '<%= appComponentPath %>';
33
import { config } from './app/app.config.server';
44

5-
const bootstrap = () => bootstrapApplication(<%= appComponentName %>, config);
6-
7-
export default bootstrap;
5+
export default bootstrapServerApplication(<%= appComponentName %>, config);

packages/schematics/angular/utility/standalone/util.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ export function getSourceFile(tree: Tree, path: string): ts.SourceFile {
5555
return source;
5656
}
5757

58-
/** Finds the call to `bootstrapApplication` within a file. */
58+
/** Finds the call to `bootstrapApplication` or `bootstrapServerApplication` within a file. */
5959
export function findBootstrapApplicationCall(tree: Tree, mainFilePath: string): ts.CallExpression {
6060
const sourceFile = getSourceFile(tree, mainFilePath);
61-
const localName = findImportLocalName(
62-
sourceFile,
63-
'bootstrapApplication',
64-
'@angular/platform-browser',
65-
);
61+
const localName =
62+
findImportLocalName(sourceFile, 'bootstrapApplication', '@angular/platform-browser') ??
63+
findImportLocalName(sourceFile, 'bootstrapServerApplication', '@angular/platform-server');
6664

6765
if (localName) {
6866
let result: ts.CallExpression | null = null;
@@ -86,7 +84,9 @@ export function findBootstrapApplicationCall(tree: Tree, mainFilePath: string):
8684
}
8785
}
8886

89-
throw new SchematicsException(`Could not find bootstrapApplication call in ${mainFilePath}`);
87+
throw new SchematicsException(
88+
`Could not find bootstrapApplication or bootstrapServerApplication call in ${mainFilePath}`,
89+
);
9090
}
9191

9292
/**

0 commit comments

Comments
 (0)