next.js-检查窗口时错误== undefined:水合失败,因为初始UI与服务器上呈现的内容不匹配

发布于 2025-01-24 04:16:58 字数 831 浏览 0 评论 0原文

我收到以下错误:

水合失败,因为初始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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2025-01-31 04:16:58

SSR的重点是在服务器上的页面上。如果在服务器上,您的页面呈现始终null无需使用SSR。

如果仅在存在会话时才要在页面上进行pererender,则必须在getserversideprops的内部检查服务器上的会话。

如果您检查页面组件内部的会话,请在客户端上完成检查,并且再次不需要使用getserversideprops

这是我一个项目中的一个示例,我在其中使用 nextauth 管理身份验证:

import { getSession } from 'next-auth/client';

// ...your page component here...

export async function getServerSideProps(context) {

  const session = await getSession({ req: context.req });

  if (session) {
    return { props: { session } };
  }
  else {
    return {
      redirect: {
        destination: '/auth',
        permanent: false,
      }
    };
  }
}

在此片段在服务器上检查了会话。
如果存在会话,它将通过其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:

import { getSession } from 'next-auth/client';

// ...your page component here...

export async function getServerSideProps(context) {

  const session = await getSession({ req: context.req });

  if (session) {
    return { props: { session } };
  }
  else {
    return {
      redirect: {
        destination: '/auth',
        permanent: false,
      }
    };
  }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文