Skip to content
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

_error.js & _app.js: getInitialProps will only be call in _app.js but not in _error.js #18249

Closed
Zauberfisch opened this issue Oct 26, 2020 · 3 comments

Comments

@Zauberfisch
Copy link

Bug report

Describe the bug

If I create both _app.js and _error.js with each having a getInitialProps method, only the getInitialProps in _app.js will be called, but never the one in _error.js

To Reproduce

// pages/_app.js
function App({Component, pageProps}) {
	return <Component {...pageProps} />
}
App.getInitialProps = async () => {
	console.log('App.getInitialProps')
	return {}
}
export default App
// pages/_error.js
function Error(props) {
	return <div>
		<h1>_error.js</h1>
		<pre>{JSON.stringify({props})}</pre>
	</div>
}
Error.getInitialProps = () => {
	console.log('Error.getInitialProps')
	return {
		thisShouldBePassedToErrorComponent: 'but it is not unless you remove App.getInitialProps'
	}
}
export default Error
// pages/index.js
export default function Home() {
	return <h1>index.js</h1>
}
export function getServerSideProps() {
	throw "error in Home.getServerSideProps"
	return {props: {}}
}
  1. visit / to get a 500 error or visit /non-existing-route to get a 404 error
  2. the console will only show 'App.getInitialProps' but never Error.getInitialProps
  3. remove App.getInitialProps from _app.js, now Error.getInitialProps will be called

Expected behavior

getInitialProps should be called in both, _app.js and _error.js

System information

  • Version of Next.js: 9.5.5
  • Version of Node.js: v14.12.0
@timneutkens
Copy link
Member

This is a bug in your code, _app getInitialProps implements getInitialProps of pages:

App.getInitialProps = async ({Component, ctx}) => {
    let pageProps = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

	return {pageProps}
}

@Zauberfisch
Copy link
Author

Ah, indeed. It's there in the comments of the custom app example to call the parent getInitialProps.

Sorry about that

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants