question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getInitialProps not running on client render

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

14.17.1

What browser are you using?

Chrome , Edge, Firefox

What operating system are you using?

Windows 10

How are you deploying your application?

Vercel

Describe the Bug

getInitialProps is running on the server but not on the client.

Expected Behavior

getInitialProps should run whenever router.push or router.replace or next/link component is used.

To Reproduce

Where the getInitialProps is called.

Login.getInitialProps = async (ctx) => {
  console.log("running");
  return {};
  // return await UnAuthenticatedRoute(ctx);
};

This is where Router is called


 React.useEffect(() => {
    if (auth) {
      if (auth.user?.verifiCationStatus != "approved") {
        Router.push("/verify");
      }
      return;
    }
    Router.replace("/login");
  }, [auth]);

When the link is called

<Link href={auth ? "/my-account" : "/login"}>
          <div>
            <div className={style.hamburgerMenuItem}></div>
            <div className={style.hamburgerMenuItem}></div>
            <div className={style.hamburgerMenuItem}></div>
          </div>
        </Link>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
masx200commented, Aug 9, 2021

https://www.nextjs.cn/docs/api-reference/data-fetching/getInitialProps

For the initial page load, getInitialProps will run on the server only. getInitialProps will then run on the client when navigating to a different route via the next/link component or by using next/router. However, if getInitialProps is used in a custom _app.js, and the page being navigated to implements getServerSideProps, then getInitialProps will run on the server.

0reactions
balazsorban44commented, Jan 27, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs getInitialProps blocked the page rendering in client side?
One solution is to check if you're on the server, and do the fetch in getInitialProps . If on the client, don't do...
Read more >
Functional _app.js causes page getInitialProps to not run #9284
The page's getInitialProps should run when the page is loaded, in this example resulting in Works! being logged to the console. System ...
Read more >
getInitialProps - Data Fetching - Next.js
Enable Server-Side Rendering in a page and do initial data population with `getInitialProps`.
Read more >
Why I Got Rid of getInitialProps in My Next.js Project
In Next.js, getInitialProps enables server-side rendering and can't be statically optimized. It runs every time we open a page.
Read more >
Next.js Server Side Rendering and getInitialProps - YouTube
We also cover some of the gotchas when working with server sid. ... will be run both on the server side as well...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found