Next.js: getInitialProps not working on reload (build)

Created on 25 Apr 2019  路  6Comments  路  Source: vercel/next.js

Bug report

Describe the bug

The getInitalProps function does not seem to be running on page reload.
I'm trying to pull the id from this path /approaches/view?id=319, it works when I navigate using links, but once I refresh the page the asPath property is now the path without the query, and the query itself is gone. Any ideas?

const ApproachView = props => <TagView {...props} type="approach" plural="approaches" />;

// ApproachView.getInitialProps = async ({ query: { id }, asPath }) => ({ id, asPath });

ApproachView.getInitialProps = async ctx => {
    console.log('get initial props ran!!');

    return { id: ctx.query.id };
};

export default ApproachView;

To Reproduce

Just reloading the page

Expected behavior

Query and path props should persist on reload

System information

  • OS: Linux
  • Browser: Chrome
  • Version of Next.js: 8.0.3

All 6 comments

Please provide a reproduction repository. It's probably something in your code but it's hard to say based on the description.

Hi @kpauletti! We're closing this because it has been a few days without a reproduction.

We hope you figured it out! If you still need help, feel free to post letting us know.

Thanks!

Hey @Timer , I actually could not figure out the issue, and I was denied posting a reproduction repo since this is company related work. I ended up just duct-taping it for now, checking for query params on reload in my componentDidMount. If I do end up running into this in future projects I'll be sure to go ahead and try to replicate on a public repo. Thanks!

    async componentDidMount() {
        if (!this.props.id || !this.props.tag) {
            const { search } = window.location;
            const { id } = queryString.parse(search);
            const idAsNum = Number(id);
            await this.setState({ id: idAsNum });
            this.fetchTag();
        }
}

@kpauletti sounds good!

I also have the same issue, if I deploy my site statically using export, getInitialProps is not calling in page reload

I also have the same issue, if I deploy my site statically using export, getInitialProps is not calling in page reload

Sounds expected, after all you did statically export the app and it was generated at build time.

Was this page helpful?
0 / 5 - 0 ratings