[英]Next.js getInitialProps - dynamic routing does not work in production
我在我的项目中使用 Next.JS 版本 9.1.3 并具有动态路由(但 static 文件与 exportPathMap)。
路由在开发模式下完美运行,但在生产模式下它不起作用。
我得到:
预呈现页面“/details/undefined”时发生错误:TypeError:无法解构“未定义”或“空”的属性
baseDetails
。
import { Component } from 'react'; import getLocks from '../../data/helper/getLocks'; import PageTemplate from '../../components/PageTemplate'; import RenderLock from '../../components/RenderLock'; class details extends Component { static async getInitialProps({ query }) { return { lock: getLocks().find((el) => el.id === query.lockID) }; } render() { const { lock } = this.props; return ( <PageTemplate> <div> <RenderLock lock={lock} /> </div> </PageTemplate> ); } } export default details;
RenderLock 使用这个:
const { baseDetails } = lock;
正如我所说,在开发模式下它可以完美运行。 baseDetails 已存在。
但是在生产模式下(如果我做了next build
和next export
)它不起作用。
在/pages
目录中包含非页面组件可能会遇到此错误。
您可以尝试将details.js
文件移动到../../components
以便它不会被识别为 static 页面。
此类错误仅next-build
执行Automatic Static Optimization步骤时可见。 相同的代码在开发模式下执行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.