英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论