fix(docs-infra): preserves query and hash when switching angular versions (#35318)

Previously, when switching angular versions through the
version selector in the sidenav, the query and hash is lost.
The user has to manually navigate to the original location again.

This commit fixes this issue and preserves the query and hash
when navigating between different versions.

Closes #24495

PR Close #35318
This commit is contained in:
Sonu Kapoor 2020-02-10 22:09:10 -05:00 committed by Alex Rickabaugh
parent c013dd4ca6
commit a788d585f8
2 changed files with 8 additions and 2 deletions

View File

@ -405,10 +405,12 @@ describe('AppComponent', () => {
// Older docs versions have an href
it('should navigate when change to a version with a url', async () => {
await setupSelectorForTesting();
locationService.urlSubject.next('new-page?id=1#section-1');
const versionWithUrlIndex = component.docVersions.findIndex(v => !!v.url);
const versionWithUrl = component.docVersions[versionWithUrlIndex];
const versionWithUrlAndPage = `${versionWithUrl.url}new-page?id=1#section-1`;
selectElement.triggerEventHandler('change', { option: versionWithUrl, index: versionWithUrlIndex});
expect(locationService.go).toHaveBeenCalledWith(versionWithUrl.url);
expect(locationService.go).toHaveBeenCalledWith(versionWithUrlAndPage);
});
it('should not navigate when change to a version without a url', async () => {

View File

@ -77,6 +77,8 @@ export class AppComponent implements OnInit {
versionInfo: VersionInfo;
private currentUrl: string;
get isOpened() { return this.isSideBySide && this.isSideNavDoc; }
get mode() { return this.isSideBySide ? 'side' : 'over'; }
@ -188,6 +190,8 @@ export class AppComponent implements OnInit {
this.navigationService.currentNodes, // ...needed to determine `sidenav` state
]).pipe(first())
.subscribe(() => this.updateShell());
this.locationService.currentUrl.subscribe(url => this.currentUrl = url);
}
onDocReady() {
@ -231,7 +235,7 @@ export class AppComponent implements OnInit {
onDocVersionChange(versionIndex: number) {
const version = this.docVersions[versionIndex];
if (version.url) {
this.locationService.go(version.url);
this.locationService.go(`${version.url}${this.currentUrl}`);
}
}