You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
useTheme throws during SSR when localStorageKey is configured #2974
useTheme({ localStorageKey: 'theme' }) throws during server-side rendering when browser localStorage is unavailable.
The hook guards window.matchMedia with isBrowser, but its theme-mode initializer still accesses localStorage.getItem whenever localStorageKey is non-empty. This remains present on current master (43003f3bd5e6921173cfa2a791ed9d34e5221935, package version 3.10.1). The earlier SSR fix #2691 protected the media query path, but not this storage path.
Minimal reproduction
Run in a server environment without browser storage:
Rendering succeeds using the existing server defaults (themeMode: 'system', theme: 'light'). Browser rendering continues to read and persist the selected mode.
Actual behavior
The initializer accesses unavailable localStorage. The regression test with localStorage explicitly unavailable throws TypeError: Cannot read properties of undefined (reading 'getItem'); runtimes without that global can throw ReferenceError instead.
A server runtime that exposes its own localStorage also causes the hook to read that storage rather than using browser-only persistence.
Proposed fix and verification
Guard storage reads and writes with the existing isBrowser utility, following the repository's useLocalStorageState pattern.
Node-environment renderToString regressions fail on the original source and pass with the guard. Browser tests verify persisted modes, normal writes, empty/omitted keys, and stable setter references. A PR with the implementation and tests will follow.
Activity