diff --git a/gatsby-ssr.js b/gatsby-ssr.js index 0a97870..33c7ee9 100644 --- a/gatsby-ssr.js +++ b/gatsby-ssr.js @@ -14,6 +14,8 @@ const ThemeScript = () => { else return "dark" } + if (typeof document === "undefined") return; + document.documentElement.style.setProperty( "--initial-theme", getInitialTheme() diff --git a/src/ThemeProvider.tsx b/src/ThemeProvider.tsx index 407b70c..372038d 100644 --- a/src/ThemeProvider.tsx +++ b/src/ThemeProvider.tsx @@ -26,9 +26,11 @@ const ThemeProvider: FC = ({ children }) => { // App's current theme const [theme, setTheme] = useState( - document.documentElement.style.getPropertyValue( - "--initial-theme" - ) as ThemeType + typeof document !== "undefined" + ? (document.documentElement.style.getPropertyValue( + "--initial-theme" + ) as ThemeType) + : "dark" ) const themeObject = useMemo(