Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cuongdevjs opened this issue Apr 14, 2020 · 6 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cuongdevjs
Copy link
Member

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

@cuongdevjs cuongdevjs changed the title when run command build i'm facing with Warning: You have opted-out of Automatic Static Optimization due to getInitialProps in pages/_app? I am looking for a solution to fixing this? when run command build i'm facing with Warning: You have opted-out of Automatic Static Optimization due to getInitialProps in pages/_app? Apr 14, 2020
@wootsbot wootsbot pinned this issue Apr 14, 2020
@wootsbot
Copy link
Member

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
Copy link
Member

@cuongdevjs the same topic is discussed here could help.

@wootsbot wootsbot added the bug Something isn't working label Apr 14, 2020
@cuongdevjs
Copy link
Member Author

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. :)

@wootsbot wootsbot added the help wanted Extra attention is needed label Apr 18, 2020
@cuongdevjs
Copy link
Member Author

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

@wootsbot
Copy link
Member

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

@cuongdevjs
Copy link
Member Author

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

Exactly. I don't know why this happens?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants