fix(platform-server): read initial location from INITIAL_CONFIG if present
This commit is contained in:
parent
047cda5b3c
commit
56f232cdd7
@ -7,13 +7,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
|
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
|
||||||
import {Inject, Injectable} from '@angular/core';
|
import {Inject, Injectable, Optional} from '@angular/core';
|
||||||
import {DOCUMENT} from '@angular/platform-browser';
|
import {DOCUMENT} from '@angular/platform-browser';
|
||||||
import {Subject} from 'rxjs/Subject';
|
import {Subject} from 'rxjs/Subject';
|
||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
|
|
||||||
import {scheduleMicroTask} from './facade/lang';
|
import {scheduleMicroTask} from './facade/lang';
|
||||||
import {getDOM} from './private_import_platform-browser';
|
import {getDOM} from './private_import_platform-browser';
|
||||||
|
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -28,7 +29,16 @@ export class ServerPlatformLocation implements PlatformLocation {
|
|||||||
private _hash: string = '';
|
private _hash: string = '';
|
||||||
private _hashUpdate = new Subject<LocationChangeEvent>();
|
private _hashUpdate = new Subject<LocationChangeEvent>();
|
||||||
|
|
||||||
constructor(@Inject(DOCUMENT) private _doc: any) {}
|
constructor(
|
||||||
|
@Inject(DOCUMENT) private _doc: any, @Optional() @Inject(INITIAL_CONFIG) _config: any) {
|
||||||
|
const config = _config as PlatformConfig | null;
|
||||||
|
if (!!config && !!config.url) {
|
||||||
|
const parsedUrl = url.parse(config.url);
|
||||||
|
this._path = parsedUrl.pathname;
|
||||||
|
this._search = parsedUrl.search;
|
||||||
|
this._hash = parsedUrl.hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc); }
|
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc); }
|
||||||
|
|
||||||
@ -59,7 +69,7 @@ export class ServerPlatformLocation implements PlatformLocation {
|
|||||||
replaceState(state: any, title: string, newUrl: string): void {
|
replaceState(state: any, title: string, newUrl: string): void {
|
||||||
const oldUrl = this.url;
|
const oldUrl = this.url;
|
||||||
const parsedUrl = url.parse(newUrl, true);
|
const parsedUrl = url.parse(newUrl, true);
|
||||||
this._path = parsedUrl.path;
|
this._path = parsedUrl.pathname;
|
||||||
this._search = parsedUrl.search;
|
this._search = parsedUrl.search;
|
||||||
this.setHash(parsedUrl.hash, oldUrl);
|
this.setHash(parsedUrl.hash, oldUrl);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export {PlatformState} from './platform_state';
|
export {PlatformState} from './platform_state';
|
||||||
export {INITIAL_CONFIG, ServerModule, platformDynamicServer, platformServer} from './server';
|
export {ServerModule, platformDynamicServer, platformServer} from './server';
|
||||||
|
export {INITIAL_CONFIG, PlatformConfig} from './tokens';
|
||||||
export {renderModule, renderModuleFactory} from './utils';
|
export {renderModule, renderModuleFactory} from './utils';
|
||||||
|
|
||||||
export * from './private_export';
|
export * from './private_export';
|
||||||
|
@ -20,6 +20,7 @@ import {ALLOW_MULTIPLE_PLATFORMS, DebugDomRendererV2, DebugDomRootRenderer} from
|
|||||||
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';
|
||||||
import {ServerStylesHost} from './styles_host';
|
import {ServerStylesHost} from './styles_host';
|
||||||
|
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
|
||||||
|
|
||||||
function notSupported(feature: string): Error {
|
function notSupported(feature: string): Error {
|
||||||
throw new Error(`platform-server does not support '${feature}'.`);
|
throw new Error(`platform-server does not support '${feature}'.`);
|
||||||
@ -65,23 +66,6 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Config object passed to initialize the platform.
|
|
||||||
*
|
|
||||||
* @experimental
|
|
||||||
*/
|
|
||||||
export interface PlatformConfig {
|
|
||||||
document?: string;
|
|
||||||
url?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The DI token for setting the initial config for the platform.
|
|
||||||
*
|
|
||||||
* @experimental
|
|
||||||
*/
|
|
||||||
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ng module for the server.
|
* The ng module for the server.
|
||||||
*
|
*
|
||||||
|
26
modules/@angular/platform-server/src/tokens.ts
Normal file
26
modules/@angular/platform-server/src/tokens.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright Google Inc. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
|
* found in the LICENSE file at https://angular.io/license
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {InjectionToken} from '@angular/core';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Config object passed to initialize the platform.
|
||||||
|
*
|
||||||
|
* @experimental
|
||||||
|
*/
|
||||||
|
export interface PlatformConfig {
|
||||||
|
document?: string;
|
||||||
|
url?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The DI token for setting the initial config for the platform.
|
||||||
|
*
|
||||||
|
* @experimental
|
||||||
|
*/
|
||||||
|
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');
|
@ -12,7 +12,8 @@ import {first} from 'rxjs/operator/first';
|
|||||||
import {toPromise} from 'rxjs/operator/toPromise';
|
import {toPromise} from 'rxjs/operator/toPromise';
|
||||||
|
|
||||||
import {PlatformState} from './platform_state';
|
import {PlatformState} from './platform_state';
|
||||||
import {INITIAL_CONFIG, platformDynamicServer, platformServer} from './server';
|
import {platformDynamicServer, platformServer} from './server';
|
||||||
|
import {INITIAL_CONFIG} from './tokens';
|
||||||
|
|
||||||
const parse5 = require('parse5');
|
const parse5 = require('parse5');
|
||||||
|
|
||||||
|
@ -162,7 +162,6 @@ export function main() {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
describe('PlatformLocation', () => {
|
describe('PlatformLocation', () => {
|
||||||
it('is injectable', async(() => {
|
it('is injectable', async(() => {
|
||||||
const platform = platformDynamicServer(
|
const platform = platformDynamicServer(
|
||||||
@ -173,6 +172,19 @@ export function main() {
|
|||||||
platform.destroy();
|
platform.destroy();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
it('is configurable via INITIAL_CONFIG', () => {
|
||||||
|
platformDynamicServer([{
|
||||||
|
provide: INITIAL_CONFIG,
|
||||||
|
useValue: {document: '<app></app>', url: 'http://test.com/deep/path?query#hash'}
|
||||||
|
}])
|
||||||
|
.bootstrapModule(ExampleModule)
|
||||||
|
.then(appRef => {
|
||||||
|
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||||
|
expect(location.pathname).toBe('/deep/path');
|
||||||
|
expect(location.search).toBe('?query');
|
||||||
|
expect(location.hash).toBe('#hash');
|
||||||
|
});
|
||||||
|
});
|
||||||
it('pushState causes the URL to update', async(() => {
|
it('pushState causes the URL to update', async(() => {
|
||||||
const platform = platformDynamicServer(
|
const platform = platformDynamicServer(
|
||||||
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
/** @experimental */
|
/** @experimental */
|
||||||
export declare const INITIAL_CONFIG: InjectionToken<PlatformConfig>;
|
export declare const INITIAL_CONFIG: InjectionToken<PlatformConfig>;
|
||||||
|
|
||||||
|
/** @experimental */
|
||||||
|
export interface PlatformConfig {
|
||||||
|
document?: string;
|
||||||
|
url?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/** @experimental */
|
/** @experimental */
|
||||||
export declare const platformDynamicServer: (extraProviders?: Provider[]) => PlatformRef;
|
export declare const platformDynamicServer: (extraProviders?: Provider[]) => PlatformRef;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user