Skip to content

Commit 056d680

Browse files
alan-agius4alxhub
authored andcommitted
feat(platform-server): add provideServerSupport function to provide server capabilities to an application (#49380)
This commit adds the `provideServerSupport()` function that returns the `EnvironmentProviders` needed to setup server rendering without NgModules. PR Close #49380
1 parent 4e098fa commit 056d680

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export class PlatformState {
4848
static ɵprov: i0.ɵɵInjectableDeclaration<PlatformState>;
4949
}
5050

51+
// @public
52+
export function provideServerSupport(): EnvironmentProviders;
53+
5154
// @public
5255
export function renderApplication<T>(bootstrap: () => Promise<ApplicationRef>, options: {
5356
document?: string | Document;

packages/platform-server/src/platform-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
export {PlatformState} from './platform_state';
10+
export {provideServerSupport} from './provide_server';
1011
export {platformDynamicServer, platformServer, ServerModule} from './server';
1112
export {BEFORE_APP_SERIALIZED, INITIAL_CONFIG, PlatformConfig} from './tokens';
1213
export {ServerTransferStateModule} from './transfer_state';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
10+
import {EnvironmentProviders, makeEnvironmentProviders} from '@angular/core';
11+
import {provideNoopAnimations} from '@angular/platform-browser/animations';
12+
13+
import {PLATFORM_SERVER_PROVIDERS} from './server';
14+
15+
/**
16+
* Sets up providers necessary to enable server rendering functionality for the application.
17+
*
18+
* @usageNotes
19+
*
20+
* Basic example of how you can add server support to your application:
21+
* ```ts
22+
* bootstrapApplication(AppComponent, {
23+
* providers: [provideServerSupport()]
24+
* });
25+
* ```
26+
*
27+
* @publicApi
28+
* @returns A set of providers to setup the server.
29+
*/
30+
export function provideServerSupport(): EnvironmentProviders {
31+
return makeEnvironmentProviders([
32+
provideHttpClient(withInterceptorsFromDi()),
33+
provideNoopAnimations(),
34+
...PLATFORM_SERVER_PROVIDERS,
35+
]);
36+
}

packages/platform-server/src/server.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
6060
{provide: EVENT_MANAGER_PLUGINS, multi: true, useClass: ServerEventManagerPlugin},
6161
];
6262

63+
export const PLATFORM_SERVER_PROVIDERS: Provider[] = [
64+
TRANSFER_STATE_SERIALIZATION_PROVIDERS,
65+
SERVER_RENDER_PROVIDERS,
66+
SERVER_HTTP_PROVIDERS,
67+
{provide: Testability, useValue: null}, // Keep for backwards-compatibility.
68+
{provide: TESTABILITY, useValue: null},
69+
{provide: ViewportScroller, useClass: NullViewportScroller},
70+
];
71+
6372
/**
6473
* The ng module for the server.
6574
*
@@ -68,14 +77,7 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
6877
@NgModule({
6978
exports: [BrowserModule],
7079
imports: [HttpClientModule, NoopAnimationsModule],
71-
providers: [
72-
TRANSFER_STATE_SERIALIZATION_PROVIDERS,
73-
SERVER_RENDER_PROVIDERS,
74-
SERVER_HTTP_PROVIDERS,
75-
{provide: Testability, useValue: null}, // Keep for backwards-compatibility.
76-
{provide: TESTABILITY, useValue: null},
77-
{provide: ViewportScroller, useClass: NullViewportScroller},
78-
],
80+
providers: PLATFORM_SERVER_PROVIDERS,
7981
})
8082
export class ServerModule {
8183
}

packages/platform-server/test/integration_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {HttpClientTestingModule, HttpTestingController} from '@angular/common/ht
1313
import {ApplicationRef, Component, destroyPlatform, getPlatform, HostListener, importProvidersFrom, Inject, inject as coreInject, Injectable, Input, NgModule, NgZone, PLATFORM_ID, ViewEncapsulation} from '@angular/core';
1414
import {TestBed, waitForAsync} from '@angular/core/testing';
1515
import {bootstrapApplication, BrowserModule, makeStateKey, Title, TransferState} from '@angular/platform-browser';
16-
import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG, platformDynamicServer, PlatformState, renderModule, ServerModule} from '@angular/platform-server';
16+
import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG, platformDynamicServer, PlatformState, provideServerSupport, renderModule, ServerModule} from '@angular/platform-server';
1717
import {Observable} from 'rxjs';
1818
import {first} from 'rxjs/operators';
1919

@@ -206,7 +206,7 @@ const MyAsyncServerAppStandalone = createMyAsyncServerApp(true);
206206
const boostrapMyAsyncServerAppStandalone = () => bootstrapApplication(MyAsyncServerAppStandalone, {
207207
providers: [
208208
importProvidersFrom(BrowserModule.withServerTransition({appId: 'simple-cmp'})),
209-
importProvidersFrom(ServerModule),
209+
provideServerSupport(),
210210
]
211211
});
212212

0 commit comments

Comments
 (0)