Created
April 5, 2025 03:34
-
-
Save JLarky/47470f3a2178b95b4ef95bc7a0e3bb0f to your computer and use it in GitHub Desktop.
nice and easy "did my React had hydration errors?" script https://x.com/JLarky/status/1908362375490867547
This file contains 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
--- | |
declare global { | |
interface Window { | |
had_hydration_error?: string; | |
} | |
} | |
--- | |
<script> | |
const prefix = 'Uncaught Error: '; | |
let hadError = false; | |
function isHydrationError(message: string) { | |
if (message.startsWith(prefix + (import.meta.env.DEV ? 'Hydration failed' : 'Minified React error #418;'))) { | |
hadError = true; | |
return 'hydration failed'; | |
} | |
if ( | |
message.startsWith( | |
prefix + | |
(import.meta.env.DEV | |
? 'This Suspense boundary received an update before it finished hydrating' | |
: 'Minified React error #421;') | |
) | |
) { | |
return 'hydration failed because of lazy components'; | |
} | |
return; | |
} | |
window.addEventListener('error', (event) => { | |
if (hadError) return; | |
const error = isHydrationError(event.message); | |
console.log('new error', error, event.message); | |
if (error) { | |
window.had_hydration_error = error; | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment