react-next-boilerplate icon indicating copy to clipboard operation
react-next-boilerplate copied to clipboard

when run command build i'm facing with `Warning: You have opted-out of Automatic Static Optimization due to getInitialProps in pages/_app`?

Open cuongdevjs opened this issue 6 years ago • 6 comments

By the next's document, this causes all pages to be executed on the server -- disabling Automatic Static Optimization.

cuongdevjs avatar Apr 14 '20 03:04 cuongdevjs

Apparently this happens because of the HOC src/utils/with-redux-store

import React from 'react';

import configureStore from './configure-store';

const isServer = typeof window === 'undefined';
const __NEXT_REDUX_STORE__ = '__NEXT_REDUX_STORE__';

function getOrCreateStore(initialState) {
  if (isServer) {
    return configureStore(initialState);
  }

  if (!window[__NEXT_REDUX_STORE__]) {
    window[__NEXT_REDUX_STORE__] = configureStore(initialState);
  }
  return window[__NEXT_REDUX_STORE__];
}

export default App => {
  return class Redux extends React.Component {
    static async getInitialProps(appContext) {
      const reduxStore = getOrCreateStore({});

      appContext.ctx.reduxStore = reduxStore;

      let appProps = {};
      if (App.getInitialProps) {
        appProps = await App.getInitialProps(appContext);
      }

      return {
        ...appProps,
        initialReduxState: reduxStore.getState(),
      };
    }

    constructor(props) {
      super(props);
      // eslint-disable-next-line react/prop-types
      this.reduxStore = getOrCreateStore(props.initialReduxState);
    }

    render() {
      return <App {...this.props} reduxStore={this.reduxStore} />;
    }
  };
};

wootsbot avatar Apr 14 '20 04:04 wootsbot

@cuongdevjs the same topic is discussed here could help.

wootsbot avatar Apr 14 '20 04:04 wootsbot

This error cause ignores auto-optimation mode of next. These pages to have no getInitialProps should be built as static HTML site but built as SSR. :)

cuongdevjs avatar Apr 14 '20 17:04 cuongdevjs

I'm facing this error when run command start: Screen Shot 2020-04-26 at 12 27 23

cuongdevjs avatar Apr 26 '20 05:04 cuongdevjs

@cuongdevjs This is happening by doing the steps of yarn build and yarn start?

wootsbot avatar Apr 26 '20 06:04 wootsbot

@cuongdevjs This is happening by doing the steps of yarn build and yarn start?

Exactly. I don't know why this happens?

cuongdevjs avatar Apr 26 '20 09:04 cuongdevjs