Skip to content

Commit

Permalink
Merge pull request #13308 from storybookjs/13273/fix-hash-dropping
Browse files Browse the repository at this point in the history
Core: Fix preview URL dropped hashes
  • Loading branch information
shilman committed Nov 27, 2020
2 parents f336ea8 + 2edf841 commit b9dde89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/core/src/client/preview/url.test.ts
Expand Up @@ -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', () => {
Expand Down
5 changes: 3 additions & 2 deletions lib/core/src/client/preview/url.ts
Expand Up @@ -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);
};

Expand Down

0 comments on commit b9dde89

Please sign in to comment.