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

Throw error if getInitialProps is defined as as instance method #4922

Merged
merged 3 commits into from
Aug 9, 2018

Conversation

HaNdTriX
Copy link
Contributor

@HaNdTriX HaNdTriX commented Aug 8, 2018

Omitting the static keyword happens pretty often. Therefore we should trigger a warning in devmode.

Closes: #4782

lib/utils.js Outdated
@@ -54,6 +54,15 @@ export function isResSent (res) {
}

export async function loadGetInitialProps (Component, ctx) {
if (
process.env.NODE_ENV !== 'production' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be a completely separate statement:

if(process.env.NODE_ENV !== 'production') {
  if(Component.prototype && Component.prototype.getInitialProps) {
    // etc
  }
}

This makes sure uglify removes it in production.

Copy link
Contributor Author

@HaNdTriX HaNdTriX Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated the PR!

@HaNdTriX HaNdTriX force-pushed the get-initial-props branch 2 times, most recently from 7e49218 to 150b892 Compare August 8, 2018 16:24
lib/utils.js Outdated
if (process.env.NODE_ENV !== 'production') {
if (Component.prototype && Component.prototype.getInitialProps) {
const compName = getDisplayName(Component)
console.warn(`"${compName}.getInitialProps()" is defined as an instance method.`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually wonder if we should throw here, there's no good use case for having getInitialProps as an instance method on a top-level component, as it will confuse both yourself and team members. Also by throwing we make sure react-error-overlay is rendered

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we throw here, this will be a major change. So we'll have to wait for next.js Version 7

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter, canary already has webpack 4 so it's going to be a major anyway 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh forgot about production statement. So I am fine with throwing. 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'll change it. Should we create an error document as well? https://github.com/zeit/next.js/tree/canary/errors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That also haha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I just fixed a few typos 😬. Now I hope everything is ok. Thanks a lot for reviewing!

@HaNdTriX HaNdTriX force-pushed the get-initial-props branch 4 times, most recently from 7133985 to 5741e75 Compare August 8, 2018 18:13
@HaNdTriX HaNdTriX changed the title Warn if getInitialProps is defined as as instance method Throw error if getInitialProps is defined as as instance method Aug 8, 2018
@timneutkens
Copy link
Member

Since we’re throwing now, can you add a test for it 👍🏻🙏🏻

}
const rejectPromise = loadGetInitialProps(TestComponent, {})
const error = new Error('"TestComponent.getInitialProps()" is defined as an instance method - visit https://err.sh/next.js/get-inital-props-as-an-instance-method for more information.')
return expect(rejectPromise).rejects.toEqual(error)
Copy link
Contributor Author

@HaNdTriX HaNdTriX Aug 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note: There is a more elegant way to test for rejected promises in jest, but this is not working with our current jest version.

Omitting the static keyword happens pretty often. Therefore we should trigger a warning in devmode.

Closes: vercel#4782
@timneutkens
Copy link
Member

timneutkens commented Aug 9, 2018

Looks great! Thanks @HaNdTriX 🙏

@timneutkens timneutkens merged commit d1b6762 into vercel:canary Aug 9, 2018
@HaNdTriX HaNdTriX deleted the get-initial-props branch August 10, 2018 10:02
@lock lock bot locked as resolved and limited conversation to collaborators Aug 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants