Skip to content

Commit 56f232c

Browse files
alxhubIgorMinar
authored andcommitted
fix(platform-server): read initial location from INITIAL_CONFIG if present
1 parent 047cda5 commit 56f232c

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

modules/@angular/platform-server/src/location.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
*/
88

99
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
10-
import {Inject, Injectable} from '@angular/core';
10+
import {Inject, Injectable, Optional} from '@angular/core';
1111
import {DOCUMENT} from '@angular/platform-browser';
1212
import {Subject} from 'rxjs/Subject';
1313
import * as url from 'url';
1414

1515
import {scheduleMicroTask} from './facade/lang';
1616
import {getDOM} from './private_import_platform-browser';
17+
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
1718

1819

1920

@@ -28,7 +29,16 @@ export class ServerPlatformLocation implements PlatformLocation {
2829
private _hash: string = '';
2930
private _hashUpdate = new Subject<LocationChangeEvent>();
3031

31-
constructor(@Inject(DOCUMENT) private _doc: any) {}
32+
constructor(
33+
@Inject(DOCUMENT) private _doc: any, @Optional() @Inject(INITIAL_CONFIG) _config: any) {
34+
const config = _config as PlatformConfig | null;
35+
if (!!config && !!config.url) {
36+
const parsedUrl = url.parse(config.url);
37+
this._path = parsedUrl.pathname;
38+
this._search = parsedUrl.search;
39+
this._hash = parsedUrl.hash;
40+
}
41+
}
3242

3343
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc); }
3444

@@ -59,7 +69,7 @@ export class ServerPlatformLocation implements PlatformLocation {
5969
replaceState(state: any, title: string, newUrl: string): void {
6070
const oldUrl = this.url;
6171
const parsedUrl = url.parse(newUrl, true);
62-
this._path = parsedUrl.path;
72+
this._path = parsedUrl.pathname;
6373
this._search = parsedUrl.search;
6474
this.setHash(parsedUrl.hash, oldUrl);
6575
}

modules/@angular/platform-server/src/platform-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
export {PlatformState} from './platform_state';
10-
export {INITIAL_CONFIG, ServerModule, platformDynamicServer, platformServer} from './server';
10+
export {ServerModule, platformDynamicServer, platformServer} from './server';
11+
export {INITIAL_CONFIG, PlatformConfig} from './tokens';
1112
export {renderModule, renderModuleFactory} from './utils';
1213

1314
export * from './private_export';

modules/@angular/platform-server/src/server.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {ALLOW_MULTIPLE_PLATFORMS, DebugDomRendererV2, DebugDomRootRenderer} from
2020
import {SharedStylesHost, getDOM} from './private_import_platform-browser';
2121
import {ServerRendererV2, ServerRootRenderer} from './server_renderer';
2222
import {ServerStylesHost} from './styles_host';
23+
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
2324

2425
function notSupported(feature: string): Error {
2526
throw new Error(`platform-server does not support '${feature}'.`);
@@ -65,23 +66,6 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
6566
},
6667
];
6768

68-
/**
69-
* Config object passed to initialize the platform.
70-
*
71-
* @experimental
72-
*/
73-
export interface PlatformConfig {
74-
document?: string;
75-
url?: string;
76-
}
77-
78-
/**
79-
* The DI token for setting the initial config for the platform.
80-
*
81-
* @experimental
82-
*/
83-
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');
84-
8569
/**
8670
* The ng module for the server.
8771
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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 {InjectionToken} from '@angular/core';
10+
11+
/**
12+
* Config object passed to initialize the platform.
13+
*
14+
* @experimental
15+
*/
16+
export interface PlatformConfig {
17+
document?: string;
18+
url?: string;
19+
}
20+
21+
/**
22+
* The DI token for setting the initial config for the platform.
23+
*
24+
* @experimental
25+
*/
26+
export const INITIAL_CONFIG = new InjectionToken<PlatformConfig>('Server.INITIAL_CONFIG');

modules/@angular/platform-server/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {first} from 'rxjs/operator/first';
1212
import {toPromise} from 'rxjs/operator/toPromise';
1313

1414
import {PlatformState} from './platform_state';
15-
import {INITIAL_CONFIG, platformDynamicServer, platformServer} from './server';
15+
import {platformDynamicServer, platformServer} from './server';
16+
import {INITIAL_CONFIG} from './tokens';
1617

1718
const parse5 = require('parse5');
1819

modules/@angular/platform-server/test/integration_spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ export function main() {
162162
});
163163
}));
164164

165-
166165
describe('PlatformLocation', () => {
167166
it('is injectable', async(() => {
168167
const platform = platformDynamicServer(
@@ -173,6 +172,19 @@ export function main() {
173172
platform.destroy();
174173
});
175174
}));
175+
it('is configurable via INITIAL_CONFIG', () => {
176+
platformDynamicServer([{
177+
provide: INITIAL_CONFIG,
178+
useValue: {document: '<app></app>', url: 'http://test.com/deep/path?query#hash'}
179+
}])
180+
.bootstrapModule(ExampleModule)
181+
.then(appRef => {
182+
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
183+
expect(location.pathname).toBe('/deep/path');
184+
expect(location.search).toBe('?query');
185+
expect(location.hash).toBe('#hash');
186+
});
187+
});
176188
it('pushState causes the URL to update', async(() => {
177189
const platform = platformDynamicServer(
178190
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);

tools/public_api_guard/platform-server/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/** @experimental */
22
export declare const INITIAL_CONFIG: InjectionToken<PlatformConfig>;
33

4+
/** @experimental */
5+
export interface PlatformConfig {
6+
document?: string;
7+
url?: string;
8+
}
9+
410
/** @experimental */
511
export declare const platformDynamicServer: (extraProviders?: Provider[]) => PlatformRef;
612

0 commit comments

Comments
 (0)