V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mightofcode
V2EX  ›  Next.js

Next.js return the 404 error page in getInitialProps

  •  
  •   mightofcode · 2021-07-01 13:01:32 +08:00 · 1199 次点击
    这是一个创建于 1001 天前的主题,其中的信息可能已经有所发展或是发生改变。

    搬运自 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']),
        }
      }
    })
    
    2 条回复    2021-07-01 14:06:50 +08:00
    summerwar
        1
    summerwar  
       2021-07-01 13:57:07 +08:00
    我觉得发帖子除了直接贴代码之外,也可以进行简单的说明
    mightofcode
        2
    mightofcode  
    OP
       2021-07-01 14:06:50 +08:00
    @summerwar 好的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1061 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:13 · PVG 03:13 · LAX 12:13 · JFK 15:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.