From a788d585f8590b40191ec318c2934fd48923897c Mon Sep 17 00:00:00 2001 From: Sonu Kapoor Date: Mon, 10 Feb 2020 22:09:10 -0500 Subject: [PATCH] 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 --- aio/src/app/app.component.spec.ts | 4 +++- aio/src/app/app.component.ts | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index 7737e04551..de93fe47c0 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -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 () => { diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index f4d65b60a8..e0df8fcce1 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -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}`); } }