堆栈:NextJS、内容丰富+现在
概述:在 getInitialProps 异步函数中调用 contentful 的 getEntries() 请求。在本地环境中,一切工作正常,我收到帖子,然后成功地将它们作为 Prop 传递到我可以渲染它们的页面。
问题:当我尝试使用 Now 进行部署时,出现此错误:
Error: The resource could not be found. at notFoundError (/zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7731:19) at /zeit/31b54c53/node_modules/contentful/dist/contentful.node.js:7854:13 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Function.module.exports.HB77.Post.getInitialProps (/zeit/31b54c53/.next/serverless/pages/p/[id].js:1198:15) at async loadGetInitialProps (/zeit/31b54c53/.next/serverless/pages/p/[id].js:3451:17) { sys: { type: 'Error', id: 'NotFound' }, details: { type: 'Entry', id: 'undefined', environment: 'master', space: undefined } } Error occurred prerendering page "/p/[id]" https://err.sh/zeit/next.js/prerender-error: Error: Failed to render serverless page at Object._default [as default] (/zeit/31b54c53/node_modules/next/dist/export/worker.js:12:212) at processTicksAndRejections (internal/process/task_queues.js:93:5)
代码:
const Post = props => {
...
}
Post.getInitialProps = async function({ query }) {
const contentfulClient = contentful.createClient({
accessToken: `${process.env.ACCESS_TOKEN}`,
space: `${process.env.SPACE}`
});
const res = await contentfulClient.getEntry(`${query.id}`);
return {
post: res
};
};
export default Post;
最佳答案
看起来您的 ENV 可能缺少:
details: { type: 'Entry', id: 'undefined', environment: 'master', space: undefined }
process.env.space
是未定义
关于reactjs - NextJS + Contentful => getInitialProps 中的 getEntries 请求破坏了生产构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59725254/