From 74d4716f6a70fbef486e1057b81d2280c40251df Mon Sep 17 00:00:00 2001 From: HiDeoo Date: Fri, 16 Aug 2024 20:32:03 +0200 Subject: Fix a sidebar persistence issue when navigating between pages with different sidebars (#2219) --- .changeset/lemon-ants-bow.md | 5 +++++ packages/starlight/components/SidebarPersistState.ts | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/lemon-ants-bow.md diff --git a/.changeset/lemon-ants-bow.md b/.changeset/lemon-ants-bow.md new file mode 100644 index 00000000..6fcba1a1 --- /dev/null +++ b/.changeset/lemon-ants-bow.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes a sidebar persistence issue when navigating between pages with different sidebar content. diff --git a/packages/starlight/components/SidebarPersistState.ts b/packages/starlight/components/SidebarPersistState.ts index 4288fa41..029dd95c 100644 --- a/packages/starlight/components/SidebarPersistState.ts +++ b/packages/starlight/components/SidebarPersistState.ts @@ -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, }; -- cgit