next.js-检查窗口时错误== undefined:水合失败,因为初始UI与服务器上呈现的内容不匹配
我收到以下错误:
水合失败,因为初始UI与服务器上呈现的内容不匹配。
我正在使用getServersideProps
,并且在我的页面中,我检查我们是否在浏览器中(window =='undefined'
),然后相应地返回事物:
export default function Page() {
const { data: session } = useSession()
if (typeof window == 'undefined') return null
if (session) {
return (
<div>
<h1>Protected Page</h1>
<p>You can view this page because you are signed in.</p>
</div>
)
}
return <p>Access Denied</p>
}
但是此行在这里此处。
if (typeof window == 'undefined') return null
似乎是问题。当这条线被评论时,一切都起作用。
的确,服务器端返回的内容将与客户端返回的内容不同 - 但是我不完全了解我应该如何解决此问题以及为什么这是一个问题?
我正在进行此检查恰恰是因为我只想在客户端 - side&amp;当我有一个会话时。我误会了吗?
I am getting the following error:
Hydration failed because the initial UI does not match what was rendered on the server.
I am using getServerSideProps
and in my page, I check whether we are in the browser (window=='undefined'
), and then return things accordingly:
export default function Page() {
const { data: session } = useSession()
if (typeof window == 'undefined') return null
if (session) {
return (
<div>
<h1>Protected Page</h1>
<p>You can view this page because you are signed in.</p>
</div>
)
}
return <p>Access Denied</p>
}
But this line here
if (typeof window == 'undefined') return null
seems to be the problem. When this line is commented out, everything works.
It's also true that what's returned on the server side will be different to whats' returned on the client side - but I don't fully understand how I should solve this problem and why this is a problem?
I am doing this check precisely because I only want to render this page on the client side -side & when I do have a session. Am I misunderstanding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SSR的重点是在服务器上的页面上。如果在服务器上,您的页面呈现始终
null
无需使用SSR。如果仅在存在会话时才要在页面上进行pererender,则必须在
getserversideprops
的内部检查服务器上的会话。如果您检查页面组件内部的会话,请在客户端上完成检查,并且再次不需要使用
getserversideprops
。这是我一个项目中的一个示例,我在其中使用 nextauth 管理身份验证:
在此片段在服务器上检查了会话。
如果存在会话,它将通过其
props
。在页面组件中访问
如果会话不存在,则将用户重定向到身份验证页面。
PS:我刚刚检查 docs 就像导入已更改为
'Next-auth/react'
(顺便说一句,在服务器上使用/client
的方式更有意义)。The point of SSR is to prerender the page on the server. If on the server your page renders always
null
there's no need to use SSR.If you want to prerender the page only if a session exists, then you have to check the session on the server, inside of
getServerSideProps
.If you check the session inside of the page component, the check is done on the client, and again there would be no need to use
getServerSideProps
.Here is an example took from one of my projects, in which I used NextAuth to manage authentication:
In this snippet the session is checked on the server.
If a session exists it will accessible in the page component via its
props
.If the session doesn't exist, the user is redirected to the authentication page.
PS: I just checked the docs and it looks like the import has changed to
'next-auth/react'
(which, by the way, make more sense as using/client
on the server is misleading).