fix(common): Update Location to get a normalized URL valid in case a represented URL starts with the substring equals APP_BASE_HREF (#48394)

```ts
@NgModule({
  imports: [RouterModule.forRoot([{path: '/enigma', component: EnigmaComponent}])],
  providers: [{provide: APP_BASE_HREF, useValue: '/en'}]
})
export class AppModule {}
```

Navigating to `/enigma` will redirect to `/en/igma` not to `/en/enigma` as it expects

Fixes: #45744

PR Close #48394
This commit is contained in:
Konstantin Kharitonov 2022-12-08 01:01:55 +01:00 committed by Jessica Janiuk
parent caa1ad000e
commit d87285c363
2 changed files with 18 additions and 1 deletions

View file

@ -301,7 +301,10 @@ export function createLocation() {
}
function _stripBasePath(basePath: string, url: string): string {
return basePath && url.startsWith(basePath) ? url.substring(basePath.length) : url;
if (basePath === url) {
return '';
}
return basePath && url.startsWith(basePath + '/') ? url.substring(basePath.length) : url;
}
function _stripIndexHtml(url: string): string {

View file

@ -266,4 +266,18 @@ describe('Location Class', () => {
expect(location.normalize(url)).toBe(route);
});
});
describe('location.normalize(url) should return correct route', () => {
it('in case url starts with the substring equals APP_BASE_HREF', () => {
const baseHref = '/en';
const url = '/enigma';
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
const location = TestBed.inject(Location);
expect(location.normalize(url)).toBe(url);
expect(location.normalize(baseHref + url)).toBe(url);
});
});
});