fix(platform-server): allow multiple instances of platformServer and platformDynamicServer
This commit is contained in:
parent
0e2fd9d91a
commit
17486fd696
|
@ -37,6 +37,8 @@ let _devMode: boolean = true;
|
||||||
let _runModeLocked: boolean = false;
|
let _runModeLocked: boolean = false;
|
||||||
let _platform: PlatformRef;
|
let _platform: PlatformRef;
|
||||||
|
|
||||||
|
export const ALLOW_MULTIPLE_PLATFORMS = new InjectionToken<boolean>('AllowMultipleToken');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disable Angular's development mode, which turns off assertions and other
|
* Disable Angular's development mode, which turns off assertions and other
|
||||||
* checks within the framework.
|
* checks within the framework.
|
||||||
|
@ -83,7 +85,8 @@ export class NgProbeToken {
|
||||||
* @experimental APIs related to application bootstrap are currently under review.
|
* @experimental APIs related to application bootstrap are currently under review.
|
||||||
*/
|
*/
|
||||||
export function createPlatform(injector: Injector): PlatformRef {
|
export function createPlatform(injector: Injector): PlatformRef {
|
||||||
if (_platform && !_platform.destroyed) {
|
if (_platform && !_platform.destroyed &&
|
||||||
|
!_platform.injector.get(ALLOW_MULTIPLE_PLATFORMS, false)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'There can be only one platform. Destroy the previous one to create a new one.');
|
'There can be only one platform. Destroy the previous one to create a new one.');
|
||||||
}
|
}
|
||||||
|
@ -103,7 +106,8 @@ export function createPlatformFactory(
|
||||||
providers: Provider[] = []): (extraProviders?: Provider[]) => PlatformRef {
|
providers: Provider[] = []): (extraProviders?: Provider[]) => PlatformRef {
|
||||||
const marker = new InjectionToken(`Platform: ${name}`);
|
const marker = new InjectionToken(`Platform: ${name}`);
|
||||||
return (extraProviders: Provider[] = []) => {
|
return (extraProviders: Provider[] = []) => {
|
||||||
if (!getPlatform()) {
|
let platform = getPlatform();
|
||||||
|
if (!platform || platform.injector.get(ALLOW_MULTIPLE_PLATFORMS, false)) {
|
||||||
if (parentPlatformFactory) {
|
if (parentPlatformFactory) {
|
||||||
parentPlatformFactory(
|
parentPlatformFactory(
|
||||||
providers.concat(extraProviders).concat({provide: marker, useValue: true}));
|
providers.concat(extraProviders).concat({provide: marker, useValue: true}));
|
||||||
|
@ -117,8 +121,7 @@ export function createPlatformFactory(
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks that there currently is a platform
|
* Checks that there currently is a platform which contains the given token as a provider.
|
||||||
* which contains the given token as a provider.
|
|
||||||
*
|
*
|
||||||
* @experimental APIs related to application bootstrap are currently under review.
|
* @experimental APIs related to application bootstrap are currently under review.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -14,6 +14,7 @@ import {AnimationSequencePlayer as AnimationSequencePlayer_} from './animation/a
|
||||||
import * as animationUtils from './animation/animation_style_util';
|
import * as animationUtils from './animation/animation_style_util';
|
||||||
import {AnimationStyles as AnimationStyles_} from './animation/animation_styles';
|
import {AnimationStyles as AnimationStyles_} from './animation/animation_styles';
|
||||||
import {AnimationTransition} from './animation/animation_transition';
|
import {AnimationTransition} from './animation/animation_transition';
|
||||||
|
import {ALLOW_MULTIPLE_PLATFORMS} from './application_ref';
|
||||||
import * as application_tokens from './application_tokens';
|
import * as application_tokens from './application_tokens';
|
||||||
import * as change_detection_util from './change_detection/change_detection_util';
|
import * as change_detection_util from './change_detection/change_detection_util';
|
||||||
import * as constants from './change_detection/constants';
|
import * as constants from './change_detection/constants';
|
||||||
|
@ -124,7 +125,8 @@ export const __core_private__: {
|
||||||
FILL_STYLE_FLAG: typeof FILL_STYLE_FLAG_,
|
FILL_STYLE_FLAG: typeof FILL_STYLE_FLAG_,
|
||||||
isPromise: typeof isPromise,
|
isPromise: typeof isPromise,
|
||||||
isObservable: typeof isObservable,
|
isObservable: typeof isObservable,
|
||||||
AnimationTransition: typeof AnimationTransition
|
AnimationTransition: typeof AnimationTransition,
|
||||||
|
ALLOW_MULTIPLE_PLATFORMS: typeof ALLOW_MULTIPLE_PLATFORMS,
|
||||||
view_utils: typeof view_utils,
|
view_utils: typeof view_utils,
|
||||||
ERROR_COMPONENT_TYPE: typeof ERROR_COMPONENT_TYPE,
|
ERROR_COMPONENT_TYPE: typeof ERROR_COMPONENT_TYPE,
|
||||||
viewEngine: typeof viewEngine,
|
viewEngine: typeof viewEngine,
|
||||||
|
@ -180,6 +182,7 @@ export const __core_private__: {
|
||||||
isPromise: isPromise,
|
isPromise: isPromise,
|
||||||
isObservable: isObservable,
|
isObservable: isObservable,
|
||||||
AnimationTransition: AnimationTransition,
|
AnimationTransition: AnimationTransition,
|
||||||
|
ALLOW_MULTIPLE_PLATFORMS: ALLOW_MULTIPLE_PLATFORMS,
|
||||||
ERROR_COMPONENT_TYPE: ERROR_COMPONENT_TYPE,
|
ERROR_COMPONENT_TYPE: ERROR_COMPONENT_TYPE,
|
||||||
TransitionEngine: TransitionEngine
|
TransitionEngine: TransitionEngine
|
||||||
} as any /* TODO(misko): export these using omega names instead */;
|
} as any /* TODO(misko): export these using omega names instead */;
|
||||||
|
|
|
@ -23,3 +23,6 @@ export type DebugDomRootRenderer = typeof r._DebugDomRootRenderer;
|
||||||
export const DebugDomRootRenderer: typeof r.DebugDomRootRenderer = r.DebugDomRootRenderer;
|
export const DebugDomRootRenderer: typeof r.DebugDomRootRenderer = r.DebugDomRootRenderer;
|
||||||
export type DebugDomRendererV2 = typeof r._DebugDomRendererV2;
|
export type DebugDomRendererV2 = typeof r._DebugDomRendererV2;
|
||||||
export const DebugDomRendererV2: typeof r.DebugDomRendererV2 = r.DebugDomRendererV2;
|
export const DebugDomRendererV2: typeof r.DebugDomRendererV2 = r.DebugDomRendererV2;
|
||||||
|
export type ALLOW_MULTIPLE_PLATFORMS = typeof r.ALLOW_MULTIPLE_PLATFORMS;
|
||||||
|
export const ALLOW_MULTIPLE_PLATFORMS: typeof r.ALLOW_MULTIPLE_PLATFORMS =
|
||||||
|
r.ALLOW_MULTIPLE_PLATFORMS;
|
||||||
|
|
|
@ -14,7 +14,7 @@ import {BrowserModule, DOCUMENT} from '@angular/platform-browser';
|
||||||
import {ServerPlatformLocation} from './location';
|
import {ServerPlatformLocation} from './location';
|
||||||
import {Parse5DomAdapter, parseDocument} from './parse5_adapter';
|
import {Parse5DomAdapter, parseDocument} from './parse5_adapter';
|
||||||
import {PlatformState} from './platform_state';
|
import {PlatformState} from './platform_state';
|
||||||
import {DebugDomRendererV2, DebugDomRootRenderer} from './private_import_core';
|
import {ALLOW_MULTIPLE_PLATFORMS, DebugDomRendererV2, DebugDomRootRenderer} from './private_import_core';
|
||||||
import {SharedStylesHost, getDOM} from './private_import_platform-browser';
|
import {SharedStylesHost, getDOM} from './private_import_platform-browser';
|
||||||
import {ServerRendererV2, ServerRootRenderer} from './server_renderer';
|
import {ServerRendererV2, ServerRootRenderer} from './server_renderer';
|
||||||
|
|
||||||
|
@ -25,8 +25,9 @@ function notSupported(feature: string): Error {
|
||||||
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
export const INTERNAL_SERVER_PLATFORM_PROVIDERS: Array<any /*Type | Provider | any[]*/> = [
|
||||||
{provide: DOCUMENT, useFactory: _document, deps: [Injector]},
|
{provide: DOCUMENT, useFactory: _document, deps: [Injector]},
|
||||||
{provide: PLATFORM_INITIALIZER, useFactory: initParse5Adapter, multi: true, deps: [Injector]},
|
{provide: PLATFORM_INITIALIZER, useFactory: initParse5Adapter, multi: true, deps: [Injector]},
|
||||||
{provide: PlatformLocation, useClass: ServerPlatformLocation},
|
{provide: PlatformLocation, useClass: ServerPlatformLocation}, PlatformState,
|
||||||
PlatformState,
|
// Add special provider that allows multiple instances of platformServer* to be created.
|
||||||
|
{provide: ALLOW_MULTIPLE_PLATFORMS, useValue: true}
|
||||||
];
|
];
|
||||||
|
|
||||||
function initParse5Adapter(injector: Injector) {
|
function initParse5Adapter(injector: Injector) {
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {ApplicationRef, NgModuleFactory, NgModuleRef, PlatformRef, Provider, Type, destroyPlatform} from '@angular/core';
|
import {ApplicationRef, NgModuleFactory, NgModuleRef, PlatformRef, Provider, Type} from '@angular/core';
|
||||||
import {filter} from 'rxjs/operator/filter';
|
import {filter} from 'rxjs/operator/filter';
|
||||||
import {first} from 'rxjs/operator/first';
|
import {first} from 'rxjs/operator/first';
|
||||||
import {toPromise} from 'rxjs/operator/toPromise';
|
import {toPromise} from 'rxjs/operator/toPromise';
|
||||||
|
@ -40,7 +40,7 @@ function _render<T>(
|
||||||
.call(first.call(filter.call(applicationRef.isStable, (isStable: boolean) => isStable)))
|
.call(first.call(filter.call(applicationRef.isStable, (isStable: boolean) => isStable)))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const output = platform.injector.get(PlatformState).renderToString();
|
const output = platform.injector.get(PlatformState).renderToString();
|
||||||
destroyPlatform();
|
platform.destroy();
|
||||||
return output;
|
return output;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,126 +25,155 @@ class MyServerApp {
|
||||||
class ExampleModule {
|
class ExampleModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Component({selector: 'app', template: `Works too!`})
|
||||||
|
class MyServerApp2 {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NgModule({declarations: [MyServerApp2], imports: [ServerModule], bootstrap: [MyServerApp2]})
|
||||||
|
class ExampleModule2 {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component({selector: 'app', template: '{{text}}'})
|
||||||
|
class MyAsyncServerApp {
|
||||||
|
text = '';
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
Promise.resolve(null).then(() => setTimeout(() => { this.text = 'Works!'; }, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NgModule(
|
||||||
|
{declarations: [MyAsyncServerApp], imports: [ServerModule], bootstrap: [MyAsyncServerApp]})
|
||||||
|
class AsyncServerModule {
|
||||||
|
}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
if (getDOM().supportsDOMEvents()) return; // NODE only
|
if (getDOM().supportsDOMEvents()) return; // NODE only
|
||||||
|
|
||||||
describe('platform-server integration', () => {
|
describe('platform-server integration', () => {
|
||||||
|
beforeEach(() => {
|
||||||
beforeEach(() => destroyPlatform());
|
if (getPlatform()) destroyPlatform();
|
||||||
afterEach(() => destroyPlatform());
|
});
|
||||||
|
|
||||||
it('should bootstrap', async(() => {
|
it('should bootstrap', async(() => {
|
||||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
const platform = platformDynamicServer(
|
||||||
.bootstrapModule(ExampleModule)
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
.then((moduleRef) => {
|
|
||||||
const doc = moduleRef.injector.get(DOCUMENT);
|
platform.bootstrapModule(ExampleModule).then((moduleRef) => {
|
||||||
expect(getDOM().getText(doc)).toEqual('Works!');
|
const doc = moduleRef.injector.get(DOCUMENT);
|
||||||
});
|
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||||
|
platform.destroy();
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('should allow multiple platform instances', async(() => {
|
||||||
|
const platform = platformDynamicServer(
|
||||||
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
|
|
||||||
|
const platform2 = platformDynamicServer(
|
||||||
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
|
|
||||||
|
|
||||||
|
platform.bootstrapModule(ExampleModule).then((moduleRef) => {
|
||||||
|
const doc = moduleRef.injector.get(DOCUMENT);
|
||||||
|
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||||
|
platform.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
platform2.bootstrapModule(ExampleModule2).then((moduleRef) => {
|
||||||
|
const doc = moduleRef.injector.get(DOCUMENT);
|
||||||
|
expect(getDOM().getText(doc)).toEqual('Works too!');
|
||||||
|
platform2.destroy();
|
||||||
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('PlatformLocation', () => {
|
describe('PlatformLocation', () => {
|
||||||
it('is injectable', () => {
|
it('is injectable', async(() => {
|
||||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
const platform = platformDynamicServer(
|
||||||
.bootstrapModule(ExampleModule)
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
.then(appRef => {
|
platform.bootstrapModule(ExampleModule).then(appRef => {
|
||||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||||
expect(location.pathname).toBe('/');
|
expect(location.pathname).toBe('/');
|
||||||
});
|
platform.destroy();
|
||||||
});
|
});
|
||||||
it('pushState causes the URL to update', () => {
|
}));
|
||||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
it('pushState causes the URL to update', async(() => {
|
||||||
.bootstrapModule(ExampleModule)
|
const platform = platformDynamicServer(
|
||||||
.then(appRef => {
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
platform.bootstrapModule(ExampleModule).then(appRef => {
|
||||||
location.pushState(null, 'Test', '/foo#bar');
|
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||||
expect(location.pathname).toBe('/foo');
|
location.pushState(null, 'Test', '/foo#bar');
|
||||||
expect(location.hash).toBe('#bar');
|
expect(location.pathname).toBe('/foo');
|
||||||
});
|
expect(location.hash).toBe('#bar');
|
||||||
});
|
platform.destroy();
|
||||||
|
});
|
||||||
|
}));
|
||||||
it('allows subscription to the hash state', done => {
|
it('allows subscription to the hash state', done => {
|
||||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
const platform =
|
||||||
.bootstrapModule(ExampleModule)
|
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
.then(appRef => {
|
platform.bootstrapModule(ExampleModule).then(appRef => {
|
||||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||||
expect(location.pathname).toBe('/');
|
expect(location.pathname).toBe('/');
|
||||||
location.onHashChange((e: any) => {
|
location.onHashChange((e: any) => {
|
||||||
expect(e.type).toBe('hashchange');
|
expect(e.type).toBe('hashchange');
|
||||||
expect(e.oldUrl).toBe('/');
|
expect(e.oldUrl).toBe('/');
|
||||||
expect(e.newUrl).toBe('/foo#bar');
|
expect(e.newUrl).toBe('/foo#bar');
|
||||||
done();
|
platform.destroy();
|
||||||
});
|
done();
|
||||||
location.pushState(null, 'Test', '/foo#bar');
|
});
|
||||||
});
|
location.pushState(null, 'Test', '/foo#bar');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('Platform Server', () => {
|
describe('render', () => {
|
||||||
@Component({selector: 'app', template: '{{text}}'})
|
let doc: string;
|
||||||
class MyAsyncServerApp {
|
let called: boolean;
|
||||||
text = '';
|
let expectedOutput =
|
||||||
|
'<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>';
|
||||||
|
|
||||||
ngOnInit() {
|
beforeEach(() => {
|
||||||
Promise.resolve(null).then(() => setTimeout(() => { this.text = 'Works!'; }, 10));
|
// PlatformConfig takes in a parsed document so that it can be cached across requests.
|
||||||
}
|
doc = '<html><head></head><body><app></app></body></html>';
|
||||||
}
|
called = false;
|
||||||
|
});
|
||||||
|
afterEach(() => { expect(called).toBe(true); });
|
||||||
|
|
||||||
@NgModule(
|
it('using long from should work', async(() => {
|
||||||
{declarations: [MyAsyncServerApp], imports: [ServerModule], bootstrap: [MyAsyncServerApp]})
|
const platform =
|
||||||
class AsyncServerModule {
|
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);
|
||||||
}
|
|
||||||
|
|
||||||
let doc: string;
|
platform.bootstrapModule(AsyncServerModule)
|
||||||
let called: boolean;
|
.then((moduleRef) => {
|
||||||
let expectedOutput =
|
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
||||||
'<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>';
|
return toPromise.call(first.call(
|
||||||
|
filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
|
||||||
|
})
|
||||||
|
.then((b) => {
|
||||||
|
expect(platform.injector.get(PlatformState).renderToString()).toBe(expectedOutput);
|
||||||
|
platform.destroy();
|
||||||
|
called = true;
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
it('using renderModule should work', async(() => {
|
||||||
destroyPlatform();
|
renderModule(AsyncServerModule, {document: doc}).then(output => {
|
||||||
// PlatformConfig takes in a parsed document so that it can be cached across requests.
|
expect(output).toBe(expectedOutput);
|
||||||
doc = '<html><head></head><body><app></app></body></html>';
|
called = true;
|
||||||
called = false;
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
it('using renderModuleFactory should work',
|
||||||
|
async(inject([PlatformRef], (defaultPlatform: PlatformRef) => {
|
||||||
|
const compilerFactory: CompilerFactory =
|
||||||
|
defaultPlatform.injector.get(CompilerFactory, null);
|
||||||
|
const moduleFactory =
|
||||||
|
compilerFactory.createCompiler().compileModuleSync(AsyncServerModule);
|
||||||
|
renderModuleFactory(moduleFactory, {document: doc}).then(output => {
|
||||||
|
expect(output).toBe(expectedOutput);
|
||||||
|
called = true;
|
||||||
|
});
|
||||||
|
})));
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
|
||||||
expect(called).toBe(true);
|
|
||||||
// Platform should have been destroyed at the end of rendering.
|
|
||||||
expect(getPlatform()).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('PlatformState should render to string (Long form rendering)', async(() => {
|
|
||||||
const platform =
|
|
||||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);
|
|
||||||
|
|
||||||
platform.bootstrapModule(AsyncServerModule)
|
|
||||||
.then((moduleRef) => {
|
|
||||||
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
|
||||||
return toPromise.call(first.call(
|
|
||||||
filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
|
|
||||||
})
|
|
||||||
.then((b) => {
|
|
||||||
expect(platform.injector.get(PlatformState).renderToString()).toBe(expectedOutput);
|
|
||||||
destroyPlatform();
|
|
||||||
called = true;
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('renderModule should render to string (short form rendering)', async(() => {
|
|
||||||
renderModule(AsyncServerModule, {document: doc}).then(output => {
|
|
||||||
expect(output).toBe(expectedOutput);
|
|
||||||
called = true;
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('renderModuleFactory should render to string (short form rendering)',
|
|
||||||
async(inject([PlatformRef], (defaultPlatform: PlatformRef) => {
|
|
||||||
const compilerFactory: CompilerFactory =
|
|
||||||
defaultPlatform.injector.get(CompilerFactory, null);
|
|
||||||
const moduleFactory =
|
|
||||||
compilerFactory.createCompiler().compileModuleSync(AsyncServerModule);
|
|
||||||
renderModuleFactory(moduleFactory, {document: doc}).then(output => {
|
|
||||||
expect(output).toBe(expectedOutput);
|
|
||||||
called = true;
|
|
||||||
});
|
|
||||||
})));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue