简体   繁体   English

Next.js getInitialProps - 动态路由在生产中不起作用

[英]Next.js getInitialProps - dynamic routing does not work in production

I use Next.JS version 9.1.3 and have dynamic routing (but static files with exportPathMap) in my project.我在我的项目中使用 Next.JS 版本 9.1.3 并具有动态路由(但 static 文件与 exportPathMap)。

The routing works perfectly in development mode, but in production mode it does not work.路由在开发模式下完美运行,但在生产模式下它不起作用。

I get:我得到:

Error occurred prerendering page "/details/undefined": TypeError: Cannot destructure property baseDetails of 'undefined' or 'null'.预呈现页面“/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;

And RenderLock uses this: RenderLock 使用这个:

const { baseDetails } = lock;

As I said, in development mode it works perfectly.正如我所说,在开发模式下它可以完美运行。 baseDetails is existing. baseDetails 已存在。

But in production mode (if I did next build and next export ) it does not work.但是在生产模式下(如果我做了next buildnext export )它不起作用。

This error can be encountered by having a non-page component inside the /pages directory./pages目录中包含非页面组件可能会遇到此错误。

You may try to move the details.js file to ../../components so it's not recognized as a static page.您可以尝试将details.js文件移动到../../components以便它不会被识别为 static 页面。

This type of error is only visible while next-build is going through the step of Automatic Static Optimization .此类错误仅next-build执行Automatic Static Optimization步骤时可见。 The same code executes fine in dev mode.相同的代码在开发模式下执行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM