next.js: Console error "Loading initial props cancelled” after redirecting inside getInitialProps on client side
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #202201092355 SMP PREEMPT Mon Jan 10 00:21:11 UTC 2022
Binaries:
Node: 16.13.1
npm: 8.18.0
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 12.3.0
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
I got the console error "Loading initial props canceled” after redirecting inside getInitialProps on the client side
import React from "react";
import Router from "next/router";
const TestPage = () => {
return <div>Test Page</div>
}
TestPage.getInitialProps = context => {
const { req, res } = context;
if (req) {
res.writeHead(302, { Location: '/other' })
res.end();
} else {
Router.push("/other");
}
return {}
}
export default TestPage;
Expected Behavior
Not getting error in console
Link to reproduction
https://codesandbox.io/s/eloquent-thompson-rkn897
To Reproduce
- run app
- go to /
- open console
- click on “Go to TEST PAGE” link
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 17
- Comments: 18 (5 by maintainers)
I have the same issue and it was introduced after I added getServerSideProps to my page. I am doing an API call and returning the response:
export const getServerSideProps = async ({ req }) => { const posts = await getAllFeedPosts(req) return { props: { posts } } }I’m having the same problem:
I’ve got a query params-based state that should be changed after a select value was changed. And this error occurs regularly - not all the time though.
I’m navigating to
/fooand changing the input, which should set thebarparameter tobaz, so we should end up at/foo?bar=baz.Is anyone working on this already?
Experiencing the same thing while doing something similar, am using
getServerSidePropslike @coderbaozi as well.@ijjk Is this error supposed to prevent the router from completing navigation? It navigates anyway and then throws after, and I’m not sure if this is intended behavior. Maybe it needs to be caught somewhere?
https://github.com/vercel/next.js/commit/122899bd37c70e12e6ac3d9503931bd0d4ac424b seems to be the change which introduced the error behavior, though according to the description here it seems like this should terminate navigation entirely. Or maybe this is a case that this change didn’t account for.
Apparently this is supposed to be a wrapper for getting initial props and is designed to fail when a certain type of navigation occurs (which calls
this.clc()above): https://github.com/vercel/next.js/blob/cbfab2a3b732a5be789f0a3a9e4c2e1177adf80e/packages/next/shared/lib/router/router.ts#L2313-L2332However, by the time we get back to the wrapper’s logic, the navigation has already been prepared (and completes after the error is thrown). This is because
changecontinues as normal after callingthis.clc().I’m not sure what needs to be done here since I don’t understand the logic in the router.