-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Conversation
be3cd41
to
80bfed7
Compare
lib/utils.js
Outdated
@@ -54,6 +54,15 @@ export function isResSent (res) { | |||
} | |||
|
|||
export async function loadGetInitialProps (Component, ctx) { | |||
if ( | |||
process.env.NODE_ENV !== 'production' && |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
7e49218
to
150b892
Compare
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.`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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. 😀
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That also haha
There was a problem hiding this comment.
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!
7133985
to
5741e75
Compare
Since we’re throwing now, can you add a test for it 👍🏻🙏🏻 |
1780a6c
to
dc8e14d
Compare
} | ||
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) |
There was a problem hiding this comment.
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
dc8e14d
to
7a0b3d7
Compare
Looks great! Thanks @HaNdTriX 🙏 |
Omitting the static keyword happens pretty often. Therefore we should trigger a warning in devmode.
Closes: #4782