From baf613eb4b428dd71a29904dc47224c5f34689f4 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 24 Jul 2019 01:15:48 +0300 Subject: [PATCH 1/2] fix(router-plugin): normalize URL by stripping base href --- packages/router-plugin/src/router.state.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/router-plugin/src/router.state.ts b/packages/router-plugin/src/router.state.ts index bdeb612ff..7b0186dcb 100644 --- a/packages/router-plugin/src/router.state.ts +++ b/packages/router-plugin/src/router.state.ts @@ -9,7 +9,7 @@ import { GuardsCheckEnd, UrlSerializer } from '@angular/router'; -import { LocationStrategy } from '@angular/common'; +import { LocationStrategy, Location } from '@angular/common'; import { Action, Selector, State, StateContext, Store } from '@ngxs/store'; import { isAngularInTestMode } from '@ngxs/store/internals'; import { filter, take } from 'rxjs/operators'; @@ -64,7 +64,8 @@ export class RouterState { private _serializer: RouterStateSerializer, private _ngZone: NgZone, private _urlSerializer: UrlSerializer, - private _locationStrategy: LocationStrategy + private _locationStrategy: LocationStrategy, + private _location: Location ) { this.setUpStoreListener(); this.setUpStateRollbackEvents(); @@ -211,8 +212,13 @@ export class RouterState { // `url` is a recognized URL by the Angular's router, while `currentUrl` is an actual URL // entered in the browser's address bar // `PathLocationStrategy.prototype.path()` returns a concatenation of - // `PlatformLocation.pathname` and normalized `PlatformLocation.search` - const currentUrl = this._locationStrategy.path(); + // `PlatformLocation.pathname` and normalized `PlatformLocation.search`. + // `Location.prototype.normalize` removes base href from the URL, + // if `baseHref` (declare in angular.json) for example is `/en` + // and the URL is `/test` - then `_locationStrategy.path()` will return `/en/test`, + // but `/en/test` is not known to the Angular's router, so we have to remove `/en` + // from the URL + const currentUrl = this._location.normalize(this._locationStrategy.path()); const currentUrlTree = this._urlSerializer.parse(currentUrl); // We need to serialize the URL because in that example `/test/?redirect=https://google.com/` // Angular will recognize it as `/test?redirect=https:%2F%2Fwww.google.com%2F` From c7fb5540f1bbe049eb79580244f6876f14775553 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 24 Jul 2019 01:18:10 +0300 Subject: [PATCH 2/2] fix(router-plugin): normalize URL by stripping base href --- packages/router-plugin/src/router.state.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/router-plugin/src/router.state.ts b/packages/router-plugin/src/router.state.ts index 7b0186dcb..fa67e737e 100644 --- a/packages/router-plugin/src/router.state.ts +++ b/packages/router-plugin/src/router.state.ts @@ -213,10 +213,11 @@ export class RouterState { // entered in the browser's address bar // `PathLocationStrategy.prototype.path()` returns a concatenation of // `PlatformLocation.pathname` and normalized `PlatformLocation.search`. - // `Location.prototype.normalize` removes base href from the URL, - // if `baseHref` (declare in angular.json) for example is `/en` + + // `Location.prototype.normalize` strips base href from the URL, + // if `baseHref` (declared in angular.json) for example is `/en` // and the URL is `/test` - then `_locationStrategy.path()` will return `/en/test`, - // but `/en/test` is not known to the Angular's router, so we have to remove `/en` + // but `/en/test` is not known to the Angular's router, so we have to strip `/en` // from the URL const currentUrl = this._location.normalize(this._locationStrategy.path()); const currentUrlTree = this._urlSerializer.parse(currentUrl);