How can I use getInitialProps only during the NextJS site build?

后端 未结 2 1095
礼貌的吻别
礼貌的吻别 2021-02-09 03:41

When using NextJS to build a static site, I would like the getInitialProps method to fire only during the build step and not on the client.

In the build ste

2条回答
  •  礼貌的吻别
    2021-02-09 03:47

    I found the workaround with NextJs 9.0.3 (other versions may also work, I didn't test that)

    // XXXPage is your page
    
    XXXPage.getInitialProps = async (req) => {
      if (process.browser) {
        return __NEXT_DATA__.props.pageProps;
      }
      // original logic
    }

提交回复
热议问题