Last active
June 30, 2022 11:35
-
-
Save KATT/13a59f3cdd443f53017694dcf3511dcd to your computer and use it in GitHub Desktop.
Prevent Flash of Unstyled Content in Chrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Next.js' `_app.js` file | |
import Head from 'next/head'; | |
import { useEffect, useState } from 'react'; | |
import '../styles/main.scss'; | |
function useIsMounted() { | |
const [mounted, setMounted] = useState(false); | |
useEffect(() => { | |
setMounted(true); | |
}, []); | |
return mounted; | |
} | |
function PreventFlashOfUnstyledContent() { | |
const mounted = useIsMounted(); | |
if (mounted) { | |
return null; | |
} | |
return ( | |
<Head> | |
<style | |
id="preventFlashOfUnstyledContent" | |
dangerouslySetInnerHTML={{ | |
__html: `*, *::before, *::after { transition: none !important; }`, | |
}} | |
/> | |
</Head> | |
); | |
} | |
const App = ({ Component, pageProps }) => { | |
return ( | |
<> | |
<PreventFlashOfUnstyledContent /> | |
<Component {...pageProps} /> | |
</> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment