例如,我们曾经使用 getServerSideProps
来重定向到页面组件中的 404 页面,如下所示:
// pages/index.js export async function getServerSideProps(context) { const placeId = context.params.placeId; const places = await getPlace(placeId); if (!places.length) { return { notFound: true, } } return { props: { places[0], }, };
通过 Next.js 13
和 app
目录,我们有了服务器组件。 getServerSideProps
不再使用时如何重定向到 404 页面?
Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1
| 本站CDN由 数掘科技 提供
根据文档,您可以使用
notFound( )
函数如下所示,它会渲染相应的not-found.js
文件:如果没有
app/user/not-found.js
文件,则使用app/not-found.js
。如果没有app/not-found.js
,它将使用 Next.js 给出的默认值。