Skip to content

Commit

Permalink
fix(common): Update Location to get a normalized URL valid in case …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
constantant authored and thePunderWoman committed Dec 12, 2022
1 parent a3fbc27 commit 59c6bfb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/common/src/location/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions packages/common/test/location/location_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 59c6bfb

Please sign in to comment.