import Document, { Head, Html, Main, NextScript } from "next/document"; class MyDocument extends Document { static async getInitialProps(ctx) { const initialProps = await Document.getInitialProps(ctx); return { ...initialProps }; } render() { // This will set the initial theme, saved in localstorage const setInitialTheme = ` function getUserPreference() { if(window.localStorage.getItem('theme')) { return window.localStorage.getItem('theme') } return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' } document.body.dataset.theme = getUserPreference(); `; return (