Next.js return the 404 error page in getInitialProps

2021-07-01 13:01:32 +08:00
 mightofcode

搬运自 stackoverrflow

直接贴代码

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']),
    }
  }
})
1245 次点击
所在节点   Next.js
2 条回复
summerwar
2021-07-01 13:57:07 +08:00
我觉得发帖子除了直接贴代码之外,也可以进行简单的说明
mightofcode
2021-07-01 14:06:50 +08:00
@summerwar 好的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/786893

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX