Skip to content

Commit 9a159e3

Browse files
EduardF1Copilot
andcommitted
fix: replace typeof window with typeof document for SSR detection
Node.js 25+ ships window as an alias for globalThis (Web Storage API experiment), which breaks the typeof window === 'undefined' SSR guard: - isServer evaluates to false on the server - ThemeScript nonce logic misbehaves server-side - getTheme() calls localStorage.getItem() on the server - Results in: TypeError: localStorage.getItem is not a function Fix: use typeof document === 'undefined' instead. document is a browser-only API that Node.js does not expose (even in v25+), making it a reliable SSR guard across all Node.js versions. Fixes #389 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a7eeabc commit 9a159e3

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

next-themes/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Attribute, ThemeProviderProps, UseThemeProps } from './types'
66

77
const colorSchemes = ['light', 'dark']
88
const MEDIA = '(prefers-color-scheme: dark)'
9-
const isServer = typeof window === 'undefined'
9+
const isServer = typeof document === 'undefined'
1010
const ThemeContext = React.createContext<UseThemeProps | undefined>(undefined)
1111
const defaultContext: UseThemeProps = { setTheme: _ => { }, themes: [] }
1212

@@ -212,7 +212,7 @@ export const ThemeScript = React.memo(
212212
<script
213213
{...scriptProps}
214214
suppressHydrationWarning
215-
nonce={typeof window === 'undefined' ? nonce : ''}
215+
nonce={typeof document === 'undefined' ? nonce : ''}
216216
dangerouslySetInnerHTML={{ __html: `(${script.toString()})(${scriptArgs})` }}
217217
/>
218218
)

0 commit comments

Comments
 (0)