Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router-plugin): normalize URL by stripping base href #1178

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/router-plugin/src/router.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -64,7 +64,8 @@ export class RouterState {
private _serializer: RouterStateSerializer<RouterStateSnapshot>,
private _ngZone: NgZone,
private _urlSerializer: UrlSerializer,
private _locationStrategy: LocationStrategy
private _locationStrategy: LocationStrategy,
private _location: Location
) {
this.setUpStoreListener();
this.setUpStateRollbackEvents();
Expand Down Expand Up @@ -211,8 +212,14 @@ 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` 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 strip `/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`
Expand Down