next.js icon indicating copy to clipboard operation
next.js copied to clipboard

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

Open timneutkens opened this issue 8 years ago • 22 comments

Bug report

Based on #3527

Describe the bug

Currently, users might add <title> in _document.js which will lead to unexpected results with next/head since _document.js is only rendered on the initial pre-render. _app.js is rendered on the server and client side, and is rendered as a normal React tree, whereas _document.js is rendered as static HTML.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Copy the default _document.js and add <title>:
// _document is only rendered on the server side and not on the client side
// Event handlers like onClick can't be added to this file

// ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document'

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <html>
        <Head>
          <title>Hello World</title>
        </Head>
        <body className="custom_class">
          <Main />
          <NextScript />
        </body>
      </html>
    )
  }
}

Expected behavior

Above example should show a warning that <title> should only be used in _app.js and other pages. This can be done using err.sh, create a link like: https://err.sh/next.js/no-document-title and create a file in the errors directory: errors/no-document-title.md

timneutkens avatar Jun 13 '18 12:06 timneutkens

Question on this issue - is including <head><title>xyz</title></head> okay? (note this is the regular head element and not the React element.

Can I also ask what's the thinking behind title being set in _app.js over _document.js? It seems (without background) to be more intuitive that I would set the title in the document.

remy avatar Jun 13 '18 16:06 remy

A title is directly correlated to a page. So it should be set in a page. <Head> from next/document is a wrapper around the real <head> tag, you shouldn't implement it yourself. The reason you should use next/head for this is that next/head managed the title and all other head elements.

timneutkens avatar Jun 13 '18 16:06 timneutkens

Hi @timneutkens, I could take this one

kvarc7 avatar Jun 15 '18 05:06 kvarc7

@davscro go ahead 👍🙌

timneutkens avatar Jun 15 '18 09:06 timneutkens

Ok, thanks. Over the weekend, between soccer matches ⚽️🇭🇷, I will take care of this task. 😀

kvarc7 avatar Jun 15 '18 09:06 kvarc7

Hi :) This is what I came to trying to solve this problem. (I was bored and tried to help 😬) However, I don't know how to go further :(

I've added isTitleExists() in Head server/document.js

isTitleExists () {
  if (this.props.children.filter(children => children.type != "title").length > 0) {
    return;
  }
  console.log("Warning: the <title> should be used only in _app.js and other pages. https://err.sh/next.js/url-deprecated")
}

Any hints?

p.s. sorry, @davscro I was bored 😃 looking forward for your solution 👍

teleginzhenya avatar Jun 16 '18 12:06 teleginzhenya

Hi @teleginzhenya, you are on the right direction but children could be an object too so your code will break. Also be careful with equality comparisons, I would suggest you to always use strict comparison 😉 @timneutkens I did it, but I think this is not a right way to fix it, IMO it should be one-time check probably during the build time, what do u think?

kvarc7 avatar Jun 19 '18 20:06 kvarc7

https://github.com/zeit/next.js/tree/canary/examples/with-styled-components

This example should be fixed?

it use <Head> tag in _document.js

@timneutkens

rainstormza avatar Oct 02 '18 04:10 rainstormza

Yes, feel free to update it @rainstormza.

I'm going to close this issue as it was fixed in #5160.

timneutkens avatar Oct 02 '18 11:10 timneutkens

@timneutkens How can we inject css-in-js on server side if we can't use<Head>tag in _document.js ?

ref: https://github.com/zeit/next.js#custom-document

rainstormza avatar Oct 04 '18 03:10 rainstormza

You can use the head tag, just not for setting as it leads to unexpected behavior</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/6324199?v=4" alt="timneutkens avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Oct 04 '18 06:10 </span> <a class="owner-name" href="http://gitmemories.com/timneutkens"> timneutkens </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>Using react helmet seems to trigger this warning.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/11298496?u=06402838c9207dce1b9d94a0501af4d12940218c&v=4" alt="zargold avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 04 '19 20:05 </span> <a class="owner-name" href="http://gitmemories.com/zargold"> zargold </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>What if I purposely want this behavior? How can I stop it from saying...</p> <pre><code>Warning: <title> should not be used in _document.js's <Head>. https://err.sh/next.js/no-document-title </code></pre> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/2536442?u=a821eaa556d3a0dfe837a69777e103ba8797eb12&v=4" alt="mattdell avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 16 '19 07:05 </span> <a class="owner-name" href="http://gitmemories.com/mattdell"> mattdell </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>import Head from 'next/head';</p> <p>from your pages e.g. index.js etc.</p> <Head> <title> cool title

itpreneur avatar Dec 15 '19 04:12 itpreneur

Hi There, I followed the error message, landing up here, moved my Head element, title and description meta tag to _app and received the following:

TypeError: Cannot read property '_documentProps' of null
    at Head.render (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:665:22)
    at processChild (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3134:18)
    at resolve (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
    at renderToString (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
    at render (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:83:16)
    at renderPage (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:406:16)
    at Object.ctx.renderPage (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:1693:28)
    at Function.getInitialProps (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:506:19)
    at Function.module.exports../src/pages/_document.js.MyDocument.getInitialProps (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:1704:85)
    at Object.loadGetInitialProps (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/lib/utils.js:59:29)
    at Object.renderToHTML (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:410:36)

Pushplaybang avatar Apr 05 '20 00:04 Pushplaybang

Hi There, I followed the error message, landing up here, moved my Head element, title and description meta tag to _app and received the following:

TypeError: Cannot read property '_documentProps' of null
    at Head.render (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:665:22)
    at processChild (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3134:18)
    at resolve (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:2960:5)
    at ReactDOMServerRenderer.render (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3435:22)
    at ReactDOMServerRenderer.read (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3373:29)
    at renderToString (/Users/paul/repositories/coronasepush-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3988:27)
    at render (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:83:16)
    at renderPage (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:406:16)
    at Object.ctx.renderPage (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:1693:28)
    at Function.getInitialProps (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:506:19)
    at Function.module.exports../src/pages/_document.js.MyDocument.getInitialProps (/Users/paul/repositories/coronasepush-web/.next/server/static/development/pages/_document.js:1704:85)
    at Object.loadGetInitialProps (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/lib/utils.js:59:29)
    at Object.renderToHTML (/Users/paul/repositories/coronasepush-web/node_modules/next/dist/next-server/server/render.js:410:36)

See https://github.com/vercel/next.js/issues/9647#issuecomment-583844001

rjerue avatar Jun 16 '20 20:06 rjerue

import Head from 'next/head';

from your pages e.g. index.js etc.

cool title

so then, if I'm understanding right, the current best practice to insert <Head> tag with <title> tags in every page, rather than jut setting it once in _document.js?

crevulus avatar Nov 13 '20 12:11 crevulus

@crevulus Afaik, you can set a "global" title in _app.tsx or in individual pages

filipesmedeiros avatar Nov 16 '20 16:11 filipesmedeiros

You should import Head from 'next/head' instead 'next/document' to put the title

rodrigogonn avatar Mar 20 '21 16:03 rodrigogonn

Hey, I'm having some problems related to this issue. When creating a _document file, it prompts the following message:

warn - Your custom Document (pages/_document) did not render all the required subcomponent. Missing component: <Head /> Read how to fix here: https://nextjs.org/docs/messages/missing-document-component

For some reason, it prompts another warning saying.

Missing required 'title' element

And obviously, I can't set the title in the _document. Is someone having the same problem? Didn't think it was enough to create a new issue.

phiberber avatar Aug 30 '21 03:08 phiberber