From 2edf84115af00305e306ec6b71b2a383c7025ba8 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Fri, 27 Nov 2020 15:13:04 +1100 Subject: [PATCH] Let's not drop hashes from preview URLs. Issue #13273 --- lib/core/src/client/preview/url.test.ts | 10 ++++++++++ lib/core/src/client/preview/url.ts | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/core/src/client/preview/url.test.ts b/lib/core/src/client/preview/url.test.ts index f9f2aa09f3ed..bc154b76cfef 100644 --- a/lib/core/src/client/preview/url.test.ts +++ b/lib/core/src/client/preview/url.test.ts @@ -42,6 +42,16 @@ describe('url', () => { 'pathname?foo=bar&id=story--id&viewMode=story' ); }); + it('should ignore + keep hashes', () => { + document.location.search = 'foo=bar&selectedStory=selStory&selectedKind=selKind'; + document.location.hash = '#foobar'; + setPath({ storyId: 'story--id', viewMode: 'story' }); + expect(history.replaceState).toHaveBeenCalledWith( + {}, + '', + 'pathname?foo=bar&id=story--id&viewMode=story#foobar' + ); + }); }); describe('parseQueryParameters', () => { diff --git a/lib/core/src/client/preview/url.ts b/lib/core/src/client/preview/url.ts index 73e89b618fd5..e49d547519d0 100644 --- a/lib/core/src/client/preview/url.ts +++ b/lib/core/src/client/preview/url.ts @@ -19,14 +19,15 @@ export const setPath = (selection?: StoreSelection) => { } const { storyId, viewMode }: { storyId: StoryId; viewMode: ViewMode } = selection; - const { path, selectedKind, selectedStory, ...rest } = qs.parse(document.location.search, { + const { search, hash } = document.location; + const { path, selectedKind, selectedStory, ...rest } = qs.parse(search, { ignoreQueryPrefix: true, }); const newPath = `${document.location.pathname}?${qs.stringify({ ...rest, id: storyId, viewMode, - })}`; + })}${hash || ''}`; history.replaceState({}, '', newPath); };