mightofcode
V2EX  ›  Next.js

Next.js return the 404 error page in getInitialProps

  •  
  •   mightofcode · Jul 1, 2021 · 2285 views
    This topic created in 1778 days ago, the information mentioned may be changed or developed.

    搬运自 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 replies    2021-07-01 14:06:50 +08:00
    summerwar
        1
    summerwar  
       Jul 1, 2021
    我觉得发帖子除了直接贴代码之外,也可以进行简单的说明
    mightofcode
        2
    mightofcode  
    OP
       Jul 1, 2021
    @summerwar 好的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5941 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 03:03 · PVG 11:03 · LAX 20:03 · JFK 23:03
    ♥ Do have faith in what you're doing.