Skip to content

Commit 68ce4f6

Browse files
constantantAndrewKushnir
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 (#48489)
```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 #48489
1 parent 20a1b43 commit 68ce4f6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/common/src/location/location.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ 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+
return basePath && new RegExp(`^${basePath}([/;?#]|$)`).test(url) ?
305+
url.substring(basePath.length) :
306+
url;
305307
}
306308

307309
function _stripIndexHtml(url: string): string {

packages/common/test/location/location_spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,25 @@ describe('Location Class', () => {
266266
expect(location.normalize(url)).toBe(route);
267267
});
268268
});
269+
270+
describe('location.normalize(url) should return properly normalized url', () => {
271+
it('in case url starts with the substring equals APP_BASE_HREF', () => {
272+
const baseHref = '/en';
273+
const path = '/enigma';
274+
const queryParams = '?param1=123';
275+
const matrixParams = ';param1=123';
276+
const fragment = '#anchor1';
277+
278+
TestBed.configureTestingModule({providers: [{provide: APP_BASE_HREF, useValue: baseHref}]});
279+
280+
const location = TestBed.inject(Location);
281+
282+
expect(location.normalize(path)).toBe(path);
283+
expect(location.normalize(baseHref)).toBe('');
284+
expect(location.normalize(baseHref + path)).toBe(path);
285+
expect(location.normalize(baseHref + queryParams)).toBe(queryParams);
286+
expect(location.normalize(baseHref + matrixParams)).toBe(matrixParams);
287+
expect(location.normalize(baseHref + fragment)).toBe(fragment);
288+
});
289+
});
269290
});

0 commit comments

Comments
 (0)