diff --git a/gatsby-ssr.js b/gatsby-ssr.js index b17b8fc..0a97870 100644 --- a/gatsby-ssr.js +++ b/gatsby-ssr.js @@ -1,7 +1,29 @@ -/** - * Implement Gatsby's SSR (Server Side Rendering) APIs in this file. - * - * See: https://www.gatsbyjs.org/docs/ssr-apis/ - */ +import React from "react" -// You can delete this file if you're not using it +const ThemeScript = () => { + const themeScript = ` +(function() { + function getInitialTheme() { + const userSetTheme = window.localStorage.getItem("theme") + const prefersLightTheme = window.matchMedia( + "(prefers-color-scheme: light)" + ) + + if (userSetTheme && userSetTheme !== '"unset"') return userSetTheme + else if (prefersLightTheme.matches === true) return "light" + else return "dark" + } + + document.documentElement.style.setProperty( + "--initial-theme", + getInitialTheme() + ) +})() + ` + + return