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

Warn if user is using <title> within the _document.js #4631

Closed
wants to merge 7 commits into from
Closed

Warn if user is using <title> within the _document.js #4631

wants to merge 7 commits into from

Conversation

davscro
Copy link
Contributor

@davscro davscro commented Jun 19, 2018

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 the constructor, what do u think?

Copy link
Member

@timneutkens timneutkens left a 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)

@davscro
Copy link
Contributor Author

davscro commented Jun 19, 2018

but {(children || []).map((child) => { is not correct as children could be a single object not an array 😉

@timneutkens
Copy link
Member

@davscro
Copy link
Contributor Author

davscro commented Jun 19, 2018

Cool, I didn't know about React.Children utils, that's why I did custom implementation. Thanks for the hint.

@timneutkens
Copy link
Member

I wonder if we can disable this in production / compile away the code

@davscro
Copy link
Contributor Author

davscro commented Sep 11, 2018

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
}

@timneutkens
Copy link
Member

There is no need for that, just use if(process.env.NODE_ENV === 'development')

@@ -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) => {
Copy link
Member

@lucleray lucleray Sep 13, 2018

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 ? 😅

Copy link
Member

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

Copy link
Member

@lucleray lucleray Sep 14, 2018

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}...

Copy link
Member

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 🤔

Copy link
Member

Choose a reason for hiding this comment

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

But only in development 🙈

Copy link

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.

Copy link
Member

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 🙂

#5160 (comment)

@davscro davscro closed this Sep 28, 2018
@davscro davscro deleted the canary_original_fork branch September 28, 2018 07:47
@lock lock bot locked as resolved and limited conversation to collaborators Oct 1, 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.

Using <title> in _document.js's <Head> tag should show a warning to use _app.js instead
4 participants