当我们构建 nextJs
yarn build
当它进入步骤 Generating static pages
时,我们遇到了这个错误:
Error occurred prerendering page "/partners/partner-with-us". Read more: https://nextjs.org/docs/messages/prerender-error
ApolloError: Dynamic server usage: Route /partners/partner-with-us couldn't be rendered statically because it used revalidate: 0 fetch http://localhost:4001 /partners/partner-with-us. See more info here: https://nextjs.org/docs/messages/dynamic-server-error
页面
partner-with-us
超级简单
const PartnerWithUs = () => {
return <>test</>
}
export default PartnerWithUs
我们使用 Apollo Graphql 来获取其他页面上的数据。我们正在使用:https://github.com/apollographql/apollo-client-nextjs?tab=readme-ov-file#in-client-components-and-streaming-ssr
在主布局中我们有
<ApolloWrapper>
<main>{children}</main>
</ApolloWrapper>
我试图解决此问题的问题:
http://localhost:4001 /partners/partner-with-us
? :4001 是后端的 url,(前端是 :4022)导致“revalidate: 0 fetch”错误的问题是我在未启用缓存的情况下获取数据。
const res = await fetch(`${this.baseUrl}/items/${entity}`, {
method: 'GET',
headers: this.getHeaders(),
cache: 'force-cache', // cache on for static generation
// cache: 'no-store', // cache off
});
我的猜测是,当没有保证缓存机制时,SSR 就会中断,从而跳过构建步骤。
不确定如何配置 apollo 客户端来启用缓存,但我会尝试查看。