Skip to content

Commit 59c6bfb

Browse files
constantantthePunderWoman
authored andcommitted
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
1 parent a3fbc27 commit 59c6bfb

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/common/src/location/location.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ export function createLocation() {
301301
}
302302

303303
function _stripBasePath(basePath: string, url: string): string {
304-
return basePath && url.startsWith(basePath) ? url.substring(basePath.length) : url;
304+
if (basePath === url) {
305+
return '';
306+
}
307+
return basePath && url.startsWith(basePath + '/') ? url.substring(basePath.length) : url;
305308
}
306309

307310
function _stripIndexHtml(url: string): string {

packages/common/test/location/location_spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,18 @@ describe('Location Class', () => {
266266
expect(location.normalize(url)).toBe(route);
267267
});
268268
});
269+
270+
describe('location.normalize(url) should return correct route', () => {
271+
it('in case url starts with the substring equals APP_BASE_HREF', () => {
272+
const baseHref = '/en';
273+
const url = '/enigma';
274+
275+
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
276+
277+
const location = TestBed.inject(Location);
278+
279+
expect(location.normalize(url)).toBe(url);
280+
expect(location.normalize(baseHref + url)).toBe(url);
281+
});
282+
});
269283
});

0 commit comments

Comments
 (0)