fix(theme): align CodeSnippets outer tab with Docusaurus storage slot#1464
Conversation
CodeSnippets used raw localStorage key docusaurus.tab.code-samples, but Docusaurus Tabs persist groupId with a namespaced key via createStorageSlot. Align reads and default tab value; prefer curl when no persisted match. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Thanks for the PR — using One suggestion before we merge: Docusaurus exports a const [persistedOuterLanguage] = useStorageSlot("docusaurus.tab.code-samples");This would replace the IIFE + What do you think about switching to |
|
Thanks — agreed. I’ll switch to useStorageSlot("docusaurus.tab.code-samples") so we rely on Docusaurus’ built-in SSR handling and stay reactive when the persisted tab changes. I’ll push an update shortly. |
Replace createStorageSlot + window guard with Docusaurus useStorageSlot for SSR-safe reads and storage reactivity. Memoize mergedLangs and sync language state when persisted tab or language list changes. Co-authored-by: Cursor <cursoragent@cursor.com>
|
I’ve updated the PR to use useStorageSlot("docusaurus.tab.code-samples") instead of the manual SSR guard + createStorageSlot().get(). I also memoized mergedLangs and added a small effect to keep language in sync when the persisted tab changes (or when the merged language list changes) so the outer tab state stays aligned with storage, not just on the initial read. Let me know if you’d like any tweaks to that sync behavior. |
|
Visit the preview URL for this PR (updated for commit 85ce80c): https://docusaurus-openapi-36b86--pr1464-vi2c1690.web.app (expires Mon, 25 May 2026 20:14:02 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: bf293780ee827f578864d92193b8c2866acd459f |
sserrata
left a comment
There was a problem hiding this comment.
Thanks for the PR — the core change (switching to useStorageSlot) is solid and improves SSR safety.
However, the curl fallback in resolveOuterLanguageFromPersistedTab is a behavior regression:
return langs.find((l) => l.language === "curl") ?? langs[0];When nothing is persisted in storage (first visit, cleared storage), this forces curl as the default regardless of the user's languageTabs ordering in docusaurus.config.js. The original code used mergedLangs[0], which respected the configured order.
Fix: change the fallback to just return langs[0]; — the array already reflects the user-defined ordering, so the first element is the correct default.
Update the default language selection logic in CodeSnippets to always return the first language in the list, removing the preference for "curl" when no match is found. This change streamlines the behavior and ensures a consistent fallback.
|
Thanks for the comment. I just updated it |
Summary
Aligns
CodeSnippetsouter language tab persistence with Docusaurus<Tabs groupId="code-samples">by reading the same namespaced storage key viacreateStorageSlot, so the selected tab and rendered snippet stay in sync after refresh.Changes
CodeSnippets): Replace rawlocalStorage.getItem('docusaurus.tab.code-samples')withcreateStorageSlot('docusaurus.tab.code-samples').get().CodeSnippets): Derive a singleinitialOuterLanguageforuseStateand the outerCodeTabsdefaultValue.CodeSnippets): When multiple languages exist and nothing valid is persisted, prefercurl, then the first language.Files Changed
packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsxTesting
npx eslint packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/CodeSnippets/index.tsx— clean locally.yarn lint/yarn test: rely on PR CI (AGENTS.md; Node 20.19+ as in CI).Related Issues
None.