Skip to content

Commit 41f27ad

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
refactor(platform-server): remove renderApplication overload that accepts a component (#49463)
This commit removes the `renderApplication` overload that accepts a root component as the first parameter. BREAKING CHANGE: `renderApplication` method no longer accepts a root component as first argument. Instead, provide a bootstrapping function that returns a `Promise<ApplicationRef>`. Before ```ts const output: string = await renderApplication(RootComponent, options); ``` Now ```ts const bootstrap = () => bootstrapApplication(RootComponent, appConfig); const output: string = await renderApplication(bootstrap, options); ``` PR Close #49463
1 parent ff9d3b0 commit 41f27ad

File tree

5 files changed

+68
-181
lines changed

5 files changed

+68
-181
lines changed

goldens/public-api/platform-server/index.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ export function renderApplication<T>(bootstrap: () => Promise<ApplicationRef>, o
5858
platformProviders?: Provider[];
5959
}): Promise<string>;
6060

61-
// @public
62-
export function renderApplication<T>(rootComponent: Type<T>, options: {
63-
appId: string;
64-
document?: string | Document;
65-
url?: string;
66-
providers?: Array<Provider | EnvironmentProviders>;
67-
platformProviders?: Provider[];
68-
}): Promise<string>;
69-
7061
// @public
7162
export function renderModule<T>(moduleType: Type<T>, options: {
7263
document?: string | Document;

packages/core/src/core_render3_private_export.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ export {
219219
export {
220220
setDocument as ɵsetDocument
221221
} from './render3/interfaces/document';
222-
export { getComponentDef as ɵgetComponentDef} from './render3/definition';
223222
export {
224223
compileComponent as ɵcompileComponent,
225224
compileDirective as ɵcompileDirective,

packages/platform-server/src/utils.ts

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ApplicationRef, EnvironmentProviders, importProvidersFrom, InjectionToken, NgModuleRef, PlatformRef, Provider, Renderer2, StaticProvider, Type, ɵannotateForHydration as annotateForHydration, ɵgetComponentDef as getComponentDef, ɵinternalCreateApplication as internalCreateApplication, ɵIS_HYDRATION_FEATURE_ENABLED as IS_HYDRATION_FEATURE_ENABLED, ɵisPromise} from '@angular/core';
10-
import {BrowserModule} from '@angular/platform-browser';
9+
import {ApplicationRef, InjectionToken, NgModuleRef, PlatformRef, Provider, Renderer2, StaticProvider, Type, ɵannotateForHydration as annotateForHydration, ɵIS_HYDRATION_FEATURE_ENABLED as IS_HYDRATION_FEATURE_ENABLED, ɵisPromise} from '@angular/core';
1110
import {first} from 'rxjs/operators';
1211

1312
import {PlatformState} from './platform_state';
14-
import {platformDynamicServer, ServerModule} from './server';
13+
import {platformDynamicServer} from './server';
1514
import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG} from './tokens';
1615

1716
interface PlatformOptions {
@@ -176,74 +175,8 @@ export function renderApplication<T>(bootstrap: () => Promise<ApplicationRef>, o
176175
document?: string|Document,
177176
url?: string,
178177
platformProviders?: Provider[],
179-
}): Promise<string>;
180-
/**
181-
* Bootstraps an instance of an Angular application and renders it to a string.
182-
*
183-
* Note: the root component passed into this function *must* be a standalone one (should have
184-
* the `standalone: true` flag in the `@Component` decorator config).
185-
*
186-
* ```typescript
187-
* @Component({
188-
* standalone: true,
189-
* template: 'Hello world!'
190-
* })
191-
* class RootComponent {}
192-
*
193-
* const output: string = await renderApplication(RootComponent, {appId: 'server-app'});
194-
* ```
195-
*
196-
* @param rootComponent A reference to a Standalone Component that should be rendered.
197-
* @param options Additional configuration for the render operation:
198-
* - `appId` - a string identifier of this application. The appId is used to prefix all
199-
* server-generated stylings and state keys of the application in TransferState
200-
* use-cases.
201-
* - `document` - the document of the page to render, either as an HTML string or
202-
* as a reference to the `document` instance.
203-
* - `url` - the URL for the current render request.
204-
* - `providers` - set of application level providers for the current render request.
205-
* - `platformProviders` - the platform level providers for the current render request.
206-
*
207-
* @returns A Promise, that returns serialized (to a string) rendered page, once resolved.
208-
*
209-
* @publicApi
210-
* @developerPreview
211-
*/
212-
export function renderApplication<T>(rootComponent: Type<T>, options: {
213-
/** @deprecated use `APP_ID` token to set the application ID. */
214-
appId: string,
215-
document?: string|Document,
216-
url?: string,
217-
providers?: Array<Provider|EnvironmentProviders>,
218-
platformProviders?: Provider[],
219-
}): Promise<string>;
220-
export function renderApplication<T>(
221-
rootComponentOrBootstrapFn: Type<T>|(() => Promise<ApplicationRef>), options: {
222-
appId?: string,
223-
document?: string|Document,
224-
url?: string,
225-
providers?: Array<Provider|EnvironmentProviders>,
226-
platformProviders?: Provider[],
227-
}): Promise<string> {
228-
const {document, url, platformProviders, appId = ''} = options;
229-
const platform = _getPlatform(platformDynamicServer, {document, url, platformProviders});
230-
231-
if (isBootstrapFn(rootComponentOrBootstrapFn)) {
232-
return _render(platform, rootComponentOrBootstrapFn());
233-
}
234-
235-
const appProviders = [
236-
importProvidersFrom(BrowserModule.withServerTransition({appId})),
237-
importProvidersFrom(ServerModule),
238-
...(options.providers ?? []),
239-
];
240-
241-
return _render(
242-
platform,
243-
internalCreateApplication({rootComponent: rootComponentOrBootstrapFn, appProviders}));
244-
}
178+
}): Promise<string> {
179+
const platform = _getPlatform(platformDynamicServer, options);
245180

246-
function isBootstrapFn(value: unknown): value is() => Promise<ApplicationRef> {
247-
// We can differentiate between a component and a bootstrap function by reading `cmp`:
248-
return typeof value === 'function' && !getComponentDef(value);
181+
return _render(platform, bootstrap());
249182
}

packages/platform-server/test/hydration_spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
import '@angular/localize/init';
1010

1111
import {CommonModule, DOCUMENT, isPlatformServer, NgComponentOutlet, NgFor, NgIf, NgTemplateOutlet} from '@angular/common';
12-
import {APP_ID, ApplicationRef, Component, ComponentRef, createComponent, destroyPlatform, Directive, ElementRef, EnvironmentInjector, getPlatform, inject, Input, PLATFORM_ID, Provider, TemplateRef, Type, ViewChild, ViewContainerRef, ɵgetComponentDef as getComponentDef, ɵprovideHydrationSupport as provideHydrationSupport, ɵsetDocument, ɵunescapeTransferStateContent as unescapeTransferStateContent} from '@angular/core';
12+
import {ApplicationRef, Component, ComponentRef, createComponent, destroyPlatform, Directive, ElementRef, EnvironmentInjector, getPlatform, inject, Input, PLATFORM_ID, Provider, TemplateRef, Type, ViewChild, ViewContainerRef, ɵprovideHydrationSupport as provideHydrationSupport, ɵsetDocument} from '@angular/core';
13+
import {getComponentDef} from '@angular/core/src/render3/definition';
14+
import {unescapeTransferStateContent} from '@angular/core/src/transfer_state';
1315
import {TestBed} from '@angular/core/testing';
1416
import {bootstrapApplication} from '@angular/platform-browser';
1517
import {first} from 'rxjs/operators';
1618

19+
import {provideServerSupport} from '../public_api';
1720
import {renderApplication} from '../src/utils';
1821

1922
/**
@@ -143,8 +146,6 @@ describe('platform-server integration', () => {
143146
afterAll(() => destroyPlatform());
144147

145148
describe('hydration', () => {
146-
const appId = 'simple-cmp';
147-
148149
let doc: Document;
149150

150151
beforeEach(() => {
@@ -168,13 +169,14 @@ describe('platform-server integration', () => {
168169
const defaultHtml = '<html><head></head><body><app></app></body></html>';
169170
const providers = [
170171
...(envProviders ?? []),
171-
{provide: APP_ID, useValue: appId},
172+
provideServerSupport(),
172173
provideHydrationSupport(),
173174
];
174-
return renderApplication(component, {
175+
176+
const bootstrap = () => bootstrapApplication(component, {providers});
177+
178+
return renderApplication(bootstrap, {
175179
document: doc ?? defaultHtml,
176-
appId,
177-
providers,
178180
});
179181
}
180182

@@ -204,10 +206,10 @@ describe('platform-server integration', () => {
204206

205207
const providers = [
206208
...(envProviders ?? []),
207-
{provide: APP_ID, useValue: appId},
208209
{provide: DOCUMENT, useFactory: _document, deps: []},
209210
provideHydrationSupport(),
210211
];
212+
211213
return bootstrapApplication(component, {providers});
212214
}
213215

0 commit comments

Comments
 (0)