-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
getInitialProps in _app.js breaks {notFound: true} in pages #23055
Comments
These are when I press the failing link. I hope these can send you in the right direction. I do think there's a bug here (although I'm not sure). Basically when post 111 fails, Next goes to post 100 for some reason. Also, the |
Nevermind, you just forgot to call the default const x = await App.getInitialProps(appContext);
const res = await fetch("https://jsonplaceholder.typicode.com/posts/100");
const footerPost = await res.json();
return { ...x, footerPost }; works |
Thanks a lot @filipesmedeiros! That indeed solved the problem (combined with Now that you mentioned it, I see that the documentation at https://nextjs.org/docs/advanced-features/custom-app also contains these two lines:
I feel like this should have been documented more explicitly, something like this: "If you implement |
Yeah I agree! |
Document the need to call App.getInitialProps() from a custom app's getInitialProps. See vercel#23055.
Closing as documentation for this was updated in #24002 |
…etInitialProps (vercel#24002) Document the need to call App.getInitialProps() from a custom app's getInitialProps. See vercel#23055.
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. |
What version of Next.js are you using?
10.0.8
What version of Node.js are you using?
14 (Code Sandbox), 15.11.0 (local machine)
What browser are you using?
Chrome
What operating system are you using?
Linux
How are you deploying your application?
intend to use next start, just local testing for now
Describe the Bug
I have a dynamic route
post/[id].js
in which I fetch data from an external API. If the API returns an error response (eg. a 404), I usereturn { notFound: true }
to render the Next.js 404 page. I also created a custom _app.js to add a header and footer to every page. This all works as expected.Now when I add a getInitialProps method to _app.js and request a post ID that does not exists, the 404 behavior breaks. Instead of the Next.js 404 page, it renders "An unexpected error has occurred."
Expected Behavior
I expect the 404 page to appear whenever my route returns
{ notFound: true }
, regardless of what happens in _app.js.To Reproduce
Go to the Code Sandbox at https://codesandbox.io/s/awesome-archimedes-fqz2g?file=/pages/_app.js:226-268. See how the behavior changes when you comment
MyApp.getInitialProps = async (appContext) => { ... }
in _app.js.The text was updated successfully, but these errors were encountered: