Next.js Runtime Error All In One
React hydration render bug
Unhandled Runtime Error
Error: Text content does not match server-rendered HTML.
Warning: Text content did not match. Server: "224828" Client: "224829"
See more info here: https://nextjs.org/docs/messages/react-hydration-error
error ❌

Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error
- Uncaught Error:
Text contentdoes not match server-rendered HTML. - Uncaught Error: Hydration failed because the
initial UIdoes not match what was rendered on the server. - Uncaught Error: There was an error while hydrating. Because the error happened outside of a
Suspense boundary, the entire root will switch to client rendering.
https://github.com/facebook/react/issues/24430
solution
Why This Error Occurred
While rendering your application, there was a difference between the React tree that was pre-rendered from the server and the React tree that was rendered during the first render in the browser (hydration).
Incorrect nestingof HTML tags- Using checks like
typeof window !== 'undefined'in your rendering logic - Using browser-only APIs like
windoworlocalStoragein your rendering logic - Browser
extensionsmodifying the HTML - Incorrectly configured
CSS-in-JSlibraries - Incorrectly configured
Edge/CDNthat attempts to modify the html response, such as Cloudflare Auto Minify
https://nextjs.org/docs/app/building-your-application/styling/css-in-js
https://github.com/vercel/next.js/tree/canary/examples
https://developers.cloudflare.com/speed/optimization/content/auto-minify/
Using
useEffectto run on theclientonly
import { useState, useEffect } from 'react'
export default function App() {
const [isClient, setIsClient] = useState(false)
useEffect(() => {
setIsClient(true)
}, [])
return <h1>{isClient ? 'This is never prerendered' : 'Prerendered'}</h1>
}
Disabling
SSRon specificcomponents
import dynamic from 'next/dynamic'
const NoSSR = dynamic(() => import('../components/no-ssr'), { ssr: false })
export default function Page() {
return (
<div>
<NoSSR />
</div>
)
}
https://nextjs.org/docs/app/building-your-application/optimizing/lazy-loading#skipping-ssr
Using
suppressHydrationWarning
<time datetime="2016-10-25" suppressHydrationWarning />
demos
"use client";
import { useState, useEffect, useRef } from "react";
function calculateModelResult(duration) {
// simulate the model calculation time, it costs 0.3s
for (var i = 0; i < 1000000000; i++) {
duration + 0;
}
return duration * 0.1;
}
export default function Page() {
const competitionStartTime = 1692956171;
const [competitionRunningTime, setCompetitionRunningTime] = useState(
Math.round(Date.now() / 1000) - competitionStartTime
);
const [modelResult, setModelResult] = useState(0);
const modelIntervalRef = useRef(0);
const competitionTimeIntervalRef = useRef(0);
const [pause, setPause] = useState(false);
const addOneSecond = () => setCompetitionRunningTime((prev) => prev + 1);
useEffect(() => {
modelIntervalRef.current = setInterval(() => {
setModelResult(calculateModelResult(competitionRunningTime));
}, 1000);
competitionTimeIntervalRef.current = setInterval(() => {
addOneSecond();
console.log(competitionRunningTime);
}, 1000);
return () => {
clearInterval(modelIntervalRef.current);
clearInterval(competitionTimeIntervalRef.current);
};
}, [competitionRunningTime]);
const pasueFunction = () => {
if (!pause) {
clearInterval(competitionTimeIntervalRef.current);
} else {
competitionTimeIntervalRef.current = setInterval(addOneSecond, 1000);
}
setPause((prev) => !prev);
};
return (
<>
{/* suppressHydrationWarning ✅ */}
{/* <h1> */}
<h1 suppressHydrationWarning>
Hello, the time of the match is {competitionRunningTime}, the model result is {modelResult}.
</h1>
<button onClick={pasueFunction}>{pause ? "Run" : "Pause"}</button>
</>
);
}
(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!