diff --git a/modules/@angular/platform-server/src/location.ts b/modules/@angular/platform-server/src/location.ts index b91a0c3f8c..896953c710 100644 --- a/modules/@angular/platform-server/src/location.ts +++ b/modules/@angular/platform-server/src/location.ts @@ -7,13 +7,14 @@ */ 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 {Subject} from 'rxjs/Subject'; import * as url from 'url'; import {scheduleMicroTask} from './facade/lang'; 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 _hashUpdate = new Subject(); - 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); } @@ -59,7 +69,7 @@ export class ServerPlatformLocation implements PlatformLocation { replaceState(state: any, title: string, newUrl: string): void { const oldUrl = this.url; const parsedUrl = url.parse(newUrl, true); - this._path = parsedUrl.path; + this._path = parsedUrl.pathname; this._search = parsedUrl.search; this.setHash(parsedUrl.hash, oldUrl); } diff --git a/modules/@angular/platform-server/src/platform-server.ts b/modules/@angular/platform-server/src/platform-server.ts index a054afa842..ffb56751c0 100644 --- a/modules/@angular/platform-server/src/platform-server.ts +++ b/modules/@angular/platform-server/src/platform-server.ts @@ -7,7 +7,8 @@ */ 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 * from './private_export'; diff --git a/modules/@angular/platform-server/src/server.ts b/modules/@angular/platform-server/src/server.ts index 2424f0e298..9a1a2ac46f 100644 --- a/modules/@angular/platform-server/src/server.ts +++ b/modules/@angular/platform-server/src/server.ts @@ -20,6 +20,7 @@ import {ALLOW_MULTIPLE_PLATFORMS, DebugDomRendererV2, DebugDomRootRenderer} from import {SharedStylesHost, getDOM} from './private_import_platform-browser'; import {ServerRendererV2, ServerRootRenderer} from './server_renderer'; import {ServerStylesHost} from './styles_host'; +import {INITIAL_CONFIG, PlatformConfig} from './tokens'; function notSupported(feature: string): Error { 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('Server.INITIAL_CONFIG'); - /** * The ng module for the server. * diff --git a/modules/@angular/platform-server/src/tokens.ts b/modules/@angular/platform-server/src/tokens.ts new file mode 100644 index 0000000000..c212a96668 --- /dev/null +++ b/modules/@angular/platform-server/src/tokens.ts @@ -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('Server.INITIAL_CONFIG'); diff --git a/modules/@angular/platform-server/src/utils.ts b/modules/@angular/platform-server/src/utils.ts index e2002b334a..0e840f0449 100644 --- a/modules/@angular/platform-server/src/utils.ts +++ b/modules/@angular/platform-server/src/utils.ts @@ -12,7 +12,8 @@ import {first} from 'rxjs/operator/first'; import {toPromise} from 'rxjs/operator/toPromise'; 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'); diff --git a/modules/@angular/platform-server/test/integration_spec.ts b/modules/@angular/platform-server/test/integration_spec.ts index 74e2ce15f3..f176ad707a 100644 --- a/modules/@angular/platform-server/test/integration_spec.ts +++ b/modules/@angular/platform-server/test/integration_spec.ts @@ -162,7 +162,6 @@ export function main() { }); })); - describe('PlatformLocation', () => { it('is injectable', async(() => { const platform = platformDynamicServer( @@ -173,6 +172,19 @@ export function main() { platform.destroy(); }); })); + it('is configurable via INITIAL_CONFIG', () => { + platformDynamicServer([{ + provide: INITIAL_CONFIG, + useValue: {document: '', 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(() => { const platform = platformDynamicServer( [{provide: INITIAL_CONFIG, useValue: {document: ''}}]); diff --git a/tools/public_api_guard/platform-server/index.d.ts b/tools/public_api_guard/platform-server/index.d.ts index de8e4ac8a8..9623845b08 100644 --- a/tools/public_api_guard/platform-server/index.d.ts +++ b/tools/public_api_guard/platform-server/index.d.ts @@ -1,6 +1,12 @@ /** @experimental */ export declare const INITIAL_CONFIG: InjectionToken; +/** @experimental */ +export interface PlatformConfig { + document?: string; + url?: string; +} + /** @experimental */ export declare const platformDynamicServer: (extraProviders?: Provider[]) => PlatformRef;