NextAuth React Server Components动态服务器使用错误

huangapple go评论69阅读模式
英文:

NextAuth React Server Components Dynamic server usage Error

问题

我一直在尝试使用React服务器组件,但是我卡在从Next Auth中检索会话数据的步骤。推荐的方法是使用getServerSession,但会引发错误:

错误:动态服务器使用:force-dynamic

根据文档,我已将动态选项设置为force-dynamic,但无论值如何,错误仍然存在。有趣的是,当在“服务器组件”路由上刷新页面时,页面可以正常加载,但当从不同页面导航到该页面时,会引发错误。

import { authOptions } from '路径/到/[...nextauth]'
export const dynamic = 'force-dynamic'
export default async function Page() {
  const session = await getServerSession(authOptions);
  return null;

更新:

如果将dynamic保留为默认值,则错误消息会更改为:

错误:动态服务器使用:headers

  14 | export default async function Page(...props: any) {
> 15 | const session = await getServerSession(authOptions);
     |                                       ^
  16 | return null;

行为保持不变,这意味着在rsc路由上正常加载,但在导航到它时会引发错误。

英文:

Been trying to use react server components, however, I'm stuck at retrieving session data from next auth. The recommended way to do this is getServerSession which throws an error:

Error: Dynamic server usage: force-dynamic

As per docs, I've set dynamic option to force-dynamic, however, the error persists regardless of the value. Interestingly enough, the page loads fine when refreshed on the "server component" route, however, when navigating to it from a different page, it throws an error

import {authOptions} from 'path/to/[...nextauth]'
export const dynamic = 'force-dynamic'
export default async function Page() {
  const session = await getServerSession(authOptions);
  return null;

UPDATE:

If dynamic is left with default value, the error message changes to:

Error: Dynamic server usage: headers

  14 | export default async function Page(...props: any) {
> 15 | const session = await getServerSession(authOptions);
     |                                       ^
  16 | return null;

the behaviour stays the same, meaning it loads fine when on the rsc route, but throws error when navigating to it

答案1

得分: 0

问题实际上是由另一个服务器组件引起的,该组件设置了我的下一个国际化并使用了generateStaticParams函数。一旦我将其设置为未定义,它就正常工作了。


function getStaticParams(){...}

export const generateStaticParams = process.env.NODE_ENV === 'production' ? getStaticParams : undefined
英文:

So, as it turns out, the issue was 'caused by another server component which sets up my next-intl and uses the generateStaticParams function. Once I set it to undefined it worked fine.


function getStaticParams(){...}

export const generateStaticParams = process.env.NODE_ENV === 'production' ? getStaticParams : undefined

huangapple
  • 本文由 发表于 2023年7月10日 13:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定