From c81d9246c4b2f5bc59aef0d7408aef2602bd6fbe Mon Sep 17 00:00:00 2001 From: Sagnik Pradhan Date: Fri, 1 Oct 2021 18:45:03 +0530 Subject: [PATCH] fix: undefined document in ssr --- gatsby-ssr.js | 2 ++ src/ThemeProvider.tsx | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) 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(