This codebase creates an "MVP" version of rehydration-safe, flicker-free Dark Mode. It's described in more detail on my blog:
https://joshwcomeau.com/gatsby/dark-mode/
You can also play with it live (though, fair warning, it's not a terribly exciting application): https://trusting-bose-add650.netlify.app
Notable files:
• gatsby-ssr.js • src/components/ThemeContext.js • src/constants.js