-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Warn if user is using <title> within the _document.js #4631
Conversation
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.
Same feedback as before: #4519 (comment)
but |
Cool, I didn't know about React.Children utils, that's why I did custom implementation. Thanks for the hint. |
I wonder if we can disable this in production / compile away the code |
hei @timneutkens , I think so. We could update https://github.com/zeit/next.js/blob/673378e415bcb3bb33f8c3bb29ad49bf29778f4a/build/webpack.js#L261-L264 to have something like: new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production'),
__DEV__: dev
}), so on the client scripts we could wrap codeblock with: if(__DEV__){
//our code which will not be included in the production bundle
} |
There is no need for that, just use |
@@ -113,7 +113,12 @@ export class Head extends Component { | |||
{this.getPreloadMainLinks()} | |||
{this.getCssLinks()} | |||
{styles || null} | |||
{this.props.children} | |||
{process.env.NODE_ENV === 'development' && (React.Children.toArray(this.props.children)).map((child) => { |
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.
Am I missing something or this.props.children
won't be rendered at all if NODE_ENV
is 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.
Correct, this is wrong, and uglify won't remove the block if it's not development. Instead we have to do something like this:
let children
if (process.env.NODE_ENV === 'development') {
children = // etc etc
} else {
children = this.props.children
}
This way uglify will remove the specific block
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.
Why not just use React.Children.forEach
to show the warning ? And keep {this.props.children}
?
if (process.env.NODE_ENV === 'development') {
React.Children.forEach(this.props.children, (child) => {
if (child.type === 'title') {
console.warn("...")
}
})
}
// return ...{this.props.children}...
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.
Doing 1 loop instead of 2 🤔
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.
But only in development
🙈
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.
Random question - is it not better practice also to check process.env.NODE_ENV !== 'production' instead? I thought that it was safer to assume that 'production' would be present in prod. Some people might want warnings to show in 'staging' also for example.
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.
Yes ! That's also how it was implemented 🙂
resolves #4596
@timneutkens I'm not sure is it right place to call this function in
render
method or IMHO it's better to place it within theconstructor
, what do u think?