英文:
Digest: DYNAMIC_SERVER_USAGE Nextjs 13
问题
错误 Next JS 13
某种原因,当我在page.tsx中获取数据时,我收到以下错误信息:Digest: DYNAMIC_SERVER_USAGE
在pages.tsx中获取数据
使用Nextjs 13.1
数据来自supabase 2
英文:
Error Next JS 13
For some reason when I fetch data in page.tsx I am getting this error : Digest: DYNAMIC_SERVER_USAGE
Fetching data in pages.tsx
Using Nextjs 13.1
Data is from supabase 2
答案1
得分: 1
我刚刚在我的page.tsx
文件中添加了export const dynamic = 'force-dynamic'
,并且它对我起作用了。
在文档 https://beta.nextjs.org/docs/api-reference/segment-config 中找到了解决方案。
英文:
I just added export const dynamic = 'force-dynamic'
to my page.tsx
file and it worked for me
Found the solution in the documentation https://beta.nextjs.org/docs/api-reference/segment-config
答案2
得分: 1
我遇到了使用cookies()函数的问题。
值得注意的是,在静态生成页面中尝试使用'next/headers'中的cookies()函数时也会出现此问题。cookies()函数允许您访问cookies,但在静态生成的上下文中使用时,它会触发相同的错误:“DynamicServerError: Dynamic server usage: headers at staticGenerationBailout”。
我的代码:
import { cookies } from "next/headers";
// 在函数内部使用如下
const supabase = createServerComponentClient({ cookies });
为了解决这个问题,我使用了以下代码:
export const dynamic = "force-dynamic";
只需在布局页面的顶部添加此行代码,问题就应该解决了。
英文:
I was facing this issue with cookies() Function Issue
It's worth noting that the issue also occurs when trying to use the cookies() function from 'next/headers' in a statically generated page. The cookies() function allows you to access cookies, but when used in the context of static generation, it triggers the same error: "DynamicServerError: Dynamic server usage: headers at staticGenerationBailout"
my code:
import { cookies } from "next/headers";
// with following inside the function
const supabase = createServerComponentClient({ cookies });
So in order to solve this:
I used:
export const dynamic = "force-dynamic";
Just add this line on top of your layout page and you should be ok
答案3
得分: 0
这是因为您为不同的用户获取不同的数据,而Next需要您明确指出这一点,这在开发模式下不会发生。
为了避免白屏的不良体验,建议添加 error.tsx 页面并使用 try catch 来处理数据获取和服务器 API。
英文:
This is because you are fetching different data for different users and Next needs you to point this out specifically, which doesn't happen in dev mode.
And to avoid the bad experience of white screen, it is recommended to add error.tsx page and use try catch to handle fetch and server api
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论