diff options
author | HiDeoo | 2024-08-16 20:32:03 +0200 |
---|---|---|
committer | GitHub | 2024-08-16 20:32:03 +0200 |
commit | 74d4716f6a70fbef486e1057b81d2280c40251df (patch) | |
tree | 2c3e7ddf3a40c9fd09b4193d44da1409d06a410d | |
parent | 96d596743eb6297ad6ef207d036fec84c332faa6 (diff) | |
download | IT.starlight-74d4716f6a70fbef486e1057b81d2280c40251df.tar.gz IT.starlight-74d4716f6a70fbef486e1057b81d2280c40251df.tar.bz2 IT.starlight-74d4716f6a70fbef486e1057b81d2280c40251df.zip |
Fix a sidebar persistence issue when navigating between pages with different sidebars (#2219)
-rw-r--r-- | .changeset/lemon-ants-bow.md | 5 | ||||
-rw-r--r-- | packages/starlight/components/SidebarPersistState.ts | 8 |
2 files changed, 10 insertions, 3 deletions
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, }; |