getInitialProps not running on client render
See original GitHub issueWhat 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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.