Skip to content

Commit

Permalink
Fix a sidebar persistence issue when navigating between pages with di…
Browse files Browse the repository at this point in the history
…fferent sidebars (#2219)
  • Loading branch information
HiDeoo authored Aug 16, 2024
1 parent 96d5967 commit 74d4716
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-ants-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes a sidebar persistence issue when navigating between pages with different sidebar content.
8 changes: 5 additions & 3 deletions packages/starlight/components/SidebarPersistState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ interface SidebarState {
/**
* Get the current sidebar state.
*
* The `open` state is loaded from session storage, while `scroll` and `hash` are read from the current page.
* The `open` state is loaded from session storage when the sidebar hashes match, while `scroll`
* and `hash` are read from the current page.
*/
const getState = (): SidebarState => {
let open = [];
const hash = target?.dataset.hash || '';
try {
const rawStoredState = sessionStorage.getItem(storageKey);
const storedState = JSON.parse(rawStoredState || '{}');
if (Array.isArray(storedState.open)) open = storedState.open;
if (Array.isArray(storedState.open) && storedState.hash === hash) open = storedState.open;
} catch {}
return {
hash: target?.dataset.hash || '',
hash,
open,
scroll: scroller?.scrollTop || 0,
};
Expand Down

0 comments on commit 74d4716

Please sign in to comment.