Skip to content

Commit 691d228

Browse files
committed
fix(sidebar): honor collapsed cookie even when localStorage is corrupt
The blocking script read the collapse cookie inside the same try as JSON.parse(localStorage); invalid persisted JSON fell through to the 248px fallback and ignored a collapsed cookie, painting an expanded-width rail on first load. Read collapse from the cookie first and parse the persisted width in its own try so the two are independent.
1 parent fc2dd87 commit 691d228

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

apps/sim/app/layout.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ export default function RootLayout({ children }: { children: React.ReactNode })
7979
var defaultSidebarWidth = 248;
8080
try {
8181
// Collapse is owned by the sidebar_collapsed cookie (the same
82-
// source the server renders structure from); width is read from
83-
// localStorage so the two never disagree on the first paint.
84-
var stored = localStorage.getItem('sidebar-state');
85-
var state = stored ? JSON.parse(stored).state : null;
82+
// source the server renders structure from) and must never depend
83+
// on localStorage parsing succeeding. Read it first, then read the
84+
// persisted width defensively in its own try.
85+
var hasCookie = document.cookie.indexOf('sidebar_collapsed=') !== -1;
8686
var collapsed = document.cookie.indexOf('sidebar_collapsed=1') !== -1;
8787
88+
var state = null;
89+
try {
90+
var stored = localStorage.getItem('sidebar-state');
91+
state = stored ? JSON.parse(stored).state : null;
92+
} catch (e) {}
93+
8894
// One-time migration: seed the cookie from the legacy localStorage
8995
// flag for users who collapsed before the cookie existed.
90-
if (document.cookie.indexOf('sidebar_collapsed=') === -1 && state && typeof state.isCollapsed === 'boolean') {
96+
if (!hasCookie && state && typeof state.isCollapsed === 'boolean') {
9197
collapsed = state.isCollapsed;
9298
document.cookie = 'sidebar_collapsed=' + (collapsed ? '1' : '0') + '; path=/; max-age=31536000; samesite=lax';
9399
}

0 commit comments

Comments
 (0)