javascript - 在 Next.JS 的 `getStaticPaths` 中设置`fallback: true` 时`throw new Error('Failed to load static props')`
问题描述
请参阅此处的讨论。我遇到了类似的错误。fallback
设置为时一切正常false
。但是,当 fallback 设置为 true 时,next js 会抛出错误
throw new Error('Failed to load static props')
解决方案
经过大量搜索和反复试验,我发现错误是由于内部抛出异常getStaticProps
。
为了解决这个问题,我所做的就是使用 try-catch 块。
export async function getStaticProps({ params }) {
let data = null;
try {
data = await getData(params.slug);
} catch (err) { };
return {
props: {
data,
},
};
并且在渲染时可以使用
if(props.data) return (<your-jsx-here></your-jsx-here>)
else return <div>Any message if you want</div>
推荐阅读
- python-3.x - python中连接失败时如何重新连接到websocket
- php - AngularJS 反序列化 PHP 字符串
- python - 多处理问题
- filesystems - 如何在 elixir 中创建相同的目录名称?
- java - OrdImage LoadFromFile 异常
- mysql - 什么是点?
- python-3.x - imaplib 的 Internaldate2tuple 期望什么输入?
- c# - c#通过动态创建的按钮控制动态创建的面板
- javascript - 网站打开时填写操作
- matplotlib - 嵌入 tkinter 的 matplotlib 动画:从未调用更新函数