目前我正在关注这个关于如何在 getInitialProps 中重定向用户的例子
https://github.com/zeit/next.js/wiki/Redirecting-in-%60getInitialProps%60
问题是,如果我想像这样返回 404,它将返回一个空白页面,而不是通常的 Next.js 404 错误页面。
context.res.writeHead(404)
context.res.end();
请注意,我知道使用 ExpressJs 和使用状态码 404 是可行的,但是,对于这个项目,我不允许使用 ExpressJs,所以我需要使用典型的 nodejs writeHead 来完成它。
最佳答案
下一个 v10 允许返回 404 页面(不带 Prop ,但如下所示)
if (!checkItem) {
return {
notFound: true
}
}
适合我的完整代码:✅✅✅
export const getServerSideProps = wrapper.getServerSideProps(async ({ req, res, locale, query, store }) => {
const { productId, categoryId } = query
const checkItem = await getProductBySlugSSR(productId, categoryId, store)
if (!checkItem) {
return { // <-----------------does the trick here!!
notFound: true
}
}
return {
props: {
...await serverSideTranslations(locale, ['common']),
}
}
})
关于javascript - Next.js 在 getInitialProps 中返回 404 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586505/