Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/MaxwellCohen/react-vite-test-command.
Something went wrong. Try again.
1.5 kB · 65 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566const logs = []const seen = new Set()
function formatArg(arg) { if (arg instanceof Error) return arg.stack || arg.message if (typeof arg === 'string') return arg try { return JSON.stringify(arg, null, 2) } catch { return String(arg) }}
function isHydrationMessage(text) { return ( /hydrat/i.test(text) || /did not match/i.test(text) || /server rendered HTML/i.test(text) || /https:\/\/react\.dev\/link\/hydration-mismatch/i.test(text) )}
function renderPanel() { const mount = document.getElementById('hydration-report') if (!mount) return
mount.hidden = logs.length === 0 if (logs.length === 0) return
mount.innerHTML = ` <h2>React hydration mismatches (${logs.length})</h2> ${logs .map( (entry) => ` <section> <h3>${entry.title}</h3> <pre>${entry.body.replace(/</g, '<')}</pre> </section> `, ) .join('')} `}
function pushLog(title, body) { const key = `${title}\n${body}` if (seen.has(key)) return seen.add(key) logs.push({ title, body }) renderPanel()}
export function captureConsoleHydration(args) { const text = args.map(formatArg).join(' ') if (!isHydrationMessage(text)) return pushLog('React console.error', text)}
export function captureRecoverableError(error, errorInfo) { const text = [formatArg(error), errorInfo?.componentStack ?? ''] .filter(Boolean) .join('\n') if (!isHydrationMessage(text)) return pushLog('React onRecoverableError', text)}