Next.js Runtime Errors All In One
Next.js Runtime Errors 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
errors ❌
- 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 content
does not match server-rendered HTML. - Uncaught Error: Hydration failed because the
initial UI
does 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
Warning: Extra attributes from the server
: data-new-gr-c-s-check-loaded,data-gr-ext-installed
app-index.js:31 Warning: Extra attributes from the server: data-new-gr-c-s-check-loaded,data-gr-ext-installed
at body
at html
at RedirectErrorBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/redirect-boundary.js:73:9)
at RedirectBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/redirect-boundary.js:81:11)
at NotFoundErrorBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/not-found-boundary.js:54:9)
at NotFoundBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/not-found-boundary.js:62:11)
at DevRootNotFoundBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/dev-root-not-found-boundary.js:32:11)
at ReactDevOverlay (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/react-dev-overlay/internal/ReactDevOverlay.js:66:9)
at HotReload (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/react-dev-overlay/hot-reloader-client.js:326:11)
at Router (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/app-router.js:147:11)
at ErrorBoundaryHandler (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/error-boundary.js:82:9)
at ErrorBoundary (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/error-boundary.js:110:11)
at AppRouter (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/components/app-router.js:417:13)
at ServerRoot (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/app-index.js:123:11)
at RSCComponent
at Root (webpack-internal:///(app-pages-browser)/./node_modules/next/dist/client/app-index.js:139:11)
https://github.com/vercel/next.js/discussions/22388#discussioncomment-6992884
- 禁用浏览器插件
- Chrome 开启无痕模式
- html / body 添加
suppressHydrationWarning={true}
solutions
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 nesting
of HTML tags- Using checks like
typeof window !== 'undefined'
in your rendering logic - Using browser-only APIs like
window
orlocalStorage
in your rendering logic - Browser
extensions
modifying the HTML - Incorrectly configured
CSS-in-JS
libraries - Incorrectly configured
Edge/CDN
that 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
useEffect
to run on theclient
only
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
SSR
on 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 />
suppressHydrationWarning={true}
✅
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body
suppressHydrationWarning={true}
className={inter.className}>
{children}
</body>
</html>
)
}
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>
</>
);
}
refs
https://zzk.cnblogs.com/my/s/blogpost-p?Keywords=telemetry
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17661326.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-08-28 LeetCode 合并两个有序链表算法题解 All In One
2022-08-28 Apple Mac clear System Data All In One
2022-08-28 磁盘读写性能测试 All In One
2021-08-28 中英文文字混排排版指南 All In One
2020-08-28 KMP 算法 & 字符串查找算法
2020-08-28 ThoughtWorks Homework
2020-08-28 js coverage testing