Skip to content

Commit ec9dc94

Browse files
dgp1130AndrewKushnir
authored andcommitted
feat(platform-browser): add context to createApplication
This is necessary to use SSR safely with `createApplication` and avoid constraining users to `bootstrapApplication`. It is one more step towards feature parity between `createApplication` and `bootstrapApplication`.
1 parent ab67988 commit ec9dc94

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class By {
5252
}
5353

5454
// @public
55-
export function createApplication(options?: ApplicationConfig): Promise<ApplicationRef>;
55+
export function createApplication(options?: ApplicationConfig, context?: BootstrapContext): Promise<ApplicationRef>;
5656

5757
// @public
5858
export function disableDebugTools(): void;

packages/platform-browser/src/browser.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ export async function bootstrapApplication(
131131
): Promise<ApplicationRef> {
132132
const config = {
133133
rootComponent,
134-
platformRef: context?.platformRef,
135-
...createProvidersConfig(options),
134+
...createProvidersConfig(options, context),
136135
};
137136

138137
if ((typeof ngJitMode === 'undefined' || ngJitMode) && typeof fetch === 'function') {
@@ -150,20 +149,27 @@ export async function bootstrapApplication(
150149
*
151150
* @param options Extra configuration for the application environment, see `ApplicationConfig` for
152151
* additional info.
152+
* @param context Optional context object that can be used to provide a pre-existing
153+
* platform injector. This is useful for advanced use-cases, for example, server-side
154+
* rendering, where the platform is created for each request.
153155
* @returns A promise that returns an `ApplicationRef` instance once resolved.
154156
*
155157
* @publicApi
156158
*/
157-
export async function createApplication(options?: ApplicationConfig): Promise<ApplicationRef> {
159+
export async function createApplication(
160+
options?: ApplicationConfig,
161+
context?: BootstrapContext,
162+
): Promise<ApplicationRef> {
158163
if ((typeof ngJitMode === 'undefined' || ngJitMode) && typeof fetch === 'function') {
159164
await resolveJitResources();
160165
}
161166

162-
return internalCreateApplication(createProvidersConfig(options));
167+
return internalCreateApplication(createProvidersConfig(options, context));
163168
}
164169

165-
function createProvidersConfig(options?: ApplicationConfig) {
170+
function createProvidersConfig(options?: ApplicationConfig, context?: BootstrapContext) {
166171
return {
172+
platformRef: context?.platformRef,
167173
appProviders: [...BROWSER_MODULE_PROVIDERS, ...(options?.providers ?? [])],
168174
platformProviders: INTERNAL_BROWSER_PLATFORM_PROVIDERS,
169175
};

packages/platform-server/test/integration_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ import {
4747
provideNgReflectAttributes,
4848
ɵSSR_CONTENT_INTEGRITY_MARKER as SSR_CONTENT_INTEGRITY_MARKER,
4949
provideZoneChangeDetection,
50+
signal,
5051
} from '@angular/core';
5152
import {TestBed} from '@angular/core/testing';
5253
import {
5354
bootstrapApplication,
55+
createApplication,
5456
BootstrapContext,
5557
BrowserModule,
5658
provideClientHydration,
@@ -767,6 +769,24 @@ class HiddenModule {}
767769
TestBed.resetTestingModule();
768770
});
769771

772+
it('should render with `createApplication`', async () => {
773+
const output = await renderApplication(
774+
async (context) => {
775+
const appRef = await createApplication(
776+
{
777+
providers: [provideZoneChangeDetection()],
778+
},
779+
context,
780+
);
781+
appRef.bootstrap(createMyAsyncServerApp(true));
782+
return appRef;
783+
},
784+
{document: doc},
785+
);
786+
787+
expect(output).toBe(expectedOutput);
788+
});
789+
770790
it('using long form should work', async () => {
771791
const platform = platformServer([
772792
{

0 commit comments

Comments
 (0)