Next.js: next/head unsetting _document <title>

Created on 2 Jan 2018  ·  16Comments  ·  Source: vercel/next.js

I'm trying to add a custom Social component to my site that adds several relevant meta tags, but it's setting the <title> to an empty string. I have the title defined in _document.js and don't change it anywhere else in the codebase. I'm running the Canary version of Next.js

import Document, { Head, Main, NextScript } from 'next/document'
export default class CustomDocument extends Document {
  static async getInitialProps (ctx) {
    return await Document.getInitialProps(ctx)
  }

  render () {
    return (
      <html lang="en">
        <Head>
          <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <title>Website Title</title>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    )
  }
}

The custom component I call looks like this:

import Head from 'next/head'

export default ({ title, image, description, url }) => (
  <Head>
    <meta key="twitter-card" name="twitter:card" content="summary" />
    <meta key="twitter-site" name="twitter:site" content="@ianmitchel1" />
    <meta key="twitter-title" name="twitter:title" content={title} />
    <meta key="twitter-description" name="twitter:description" content={description} />
    <meta key="twitter-image" name="twitter:image" content={image} />
    <meta key="twitter-url" name="twitter:url" content={url} />

    <meta key="facebook-type" property="og:type" content="article" />
    <meta key="facebook-title" property="og:title" content={title} />
    <meta key="facebook-description" property="og:description" content={description} />
    <meta key="facebook-image" property="og:image" content={image} />
    <meta key="facebook-url" property="og:url" content={url} />
  </Head>
)

It adds all the meta tags correctly (not sure if it's related, but I needed to add the key parameter - otherwise only the second set of meta tags was added) and the other meta tags from _document.js remain intact, but somehow the title tag just becomes <title></title>.

I've created a small example here - https://next-title-pnxpfydvvh.now.sh

bug

Most helpful comment

This is quite critical and still present. Any news?

All 16 comments

Same issue, running 4.2.1

I had this problem with 4.3.0-canary.1

It has been corrected as of 4.4.0-canary.3

This is indeed a bug. This line show be wrapped in the if statement. I'll fix it once I'm done with universal webpack
https://github.com/zeit/next.js/blob/9320d9f006164f2e997982ce76e80122049ccb3c/client/head-manager.js#L33

@timneutkens I can make PR for this if you don't mind.

@Sekhmet sure 👍 Please add tests for it 👍

I've just come across this and it's still present in [email protected]. Example here: https://next-title-bug-kraxvxvjas.now.sh - source

This is quite critical and still present. Any news?

Same here, title and description appears in first glance but instantly goes away.
@Sekhmet will you fix this? @timneutkens Any plans to release it?

still an issue in 0.5.1, when will the fix be merged?

Chiming in to say this appears to still be an issue. React is logging the correct value for on render, but it appears blank after that.</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars3.githubusercontent.com/u/2423322?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="ewagstaff picture"> <strong>ewagstaff</strong> <span class="text-muted ml-1">on 10 May 2018</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="mb-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3731713875" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>what seems to have worked for me was to add another Head with just the title in my component. weird seeing everything else from _document.js is there</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/15183881?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="JonathonJulian picture"> <strong>JonathonJulian</strong> <span class="text-muted ml-1">on 17 May 2018</span> </div> <div class="col text-right"> 👍<span class="ml-2 mr-3">3</span> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>Having this issue in 5.01-canary.17. For now I will just duplicate the head on the pages that I need. Please fix one day :)</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars1.githubusercontent.com/u/20288136?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="wdifruscio picture"> <strong>wdifruscio</strong> <span class="text-muted ml-1">on 17 May 2018</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>JonathonJulian's solution seems pretty hacky. And while adding another title tag does leave the 2nd title tag intact, the first one is still getting emptied. In Chrome at least, the first one is what gets checked.</p> <p>Is there a timetable on this being fixed for real?</p> <p>EDIT: It does actually work ok if it's placed inside a head tag on the page level (e.g. index.js) but not if it's placed in _document.js or any other component. Does this error stem from letting individual pages overwrite the _document.js title?</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars3.githubusercontent.com/u/2423322?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="ewagstaff picture"> <strong>ewagstaff</strong> <span class="text-muted ml-1">on 24 May 2018</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>Agreed, its not really a solution, a workaround at best, I ended up removing the title in _document as both tags were being picked up by google web crawlers</p> <p>My site is pretty basic so not a big deal for me to to add to the pages and its better than no title, but still… pls fix, surprised this was fixed in 6.0??</p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/15183881?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="JonathonJulian picture"> <strong>JonathonJulian</strong> <span class="text-muted ml-1">on 24 May 2018</span> </div> <div class="col text-right"> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>See my comments here: <a rel="nofollow noopener" target="_blank" href="https://github.com/zeit/next.js/pull/3795">https://github.com/zeit/next.js/pull/3795</a></p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/6324199?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="timneutkens picture"> <strong>timneutkens</strong> <span class="text-muted ml-1">on 25 May 2018</span> </div> <div class="col text-right"> 👍<span class="ml-2 mr-3">1</span> </div> </div> </div> </div> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown"> <p>Closing this in favor of #4596 based on my comments in #3795 </p> </div> <div class="card-footer"> <div class="row"> <div class="col"> <img src="https://avatars2.githubusercontent.com/u/6324199?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="timneutkens picture"> <strong>timneutkens</strong> <span class="text-muted ml-1">on 13 Jun 2018</span> </div> <div class="col text-right"> </div> </div> </div> </div> </div> <div class="col-12"> <div class="card card-custom mb-4"> <div class="card-body pt-3 pb-3 markdown text-center helpful"> <div class="title">Was this page helpful?</div> <div class="mt-1" onMouseLeave="rating(285499816, 0);"> <i class="fas fa-star inactive" id="star-1" onMouseOver="rating(285499816, 1);" onclick="rate(285499816, 1);"></i> <i class="fas fa-star inactive" id="star-2" onMouseOver="rating(285499816, 2);" onclick="rate(285499816, 2);"></i> <i class="fas fa-star inactive" id="star-3" onMouseOver="rating(285499816, 3);" onclick="rate(285499816, 3);"></i> <i class="fas fa-star inactive" id="star-4" onMouseOver="rating(285499816, 4);" onclick="rate(285499816, 4);"></i> <i class="fas fa-star inactive" id="star-5" onMouseOver="rating(285499816, 5);" onclick="rate(285499816, 5);"></i> </div> <div class="description text-small"><span id="rating-val">0</span> / 5 - <span id="rating-count">0</span> ratings</div> </div> </div> <div class="mb-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3452512275" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> </div> <div class="col-12 col-lg-4"> <div id="ph-above-related"></div> <div class="card card-custom issue-box"> <div class="card-body pt-3 pb-5"> <h2 class="mb-4">Related issues</h2> <div> <strong> <a href="/next-js/186135351/adding-ga-script-tag">Adding GA script tag?</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/238295?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="robinvdvleuten picture"> <strong class="pr-1" dir="ltr">robinvdvleuten</strong>  ·  <span class="px-1" dir="ltr">74</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/next-js/476991825/next-js-api-routes-and-pages-should-support-reading-files">Next.js API routes (and pages) should support reading files</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars1.githubusercontent.com/u/616428?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="Timer picture"> <strong class="pr-1" dir="ltr">Timer</strong>  ·  <span class="px-1" dir="ltr">87</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/next-js/489381964/rfc-css-support">[RFC] CSS Support</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars1.githubusercontent.com/u/616428?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="Timer picture"> <strong class="pr-1" dir="ltr">Timer</strong>  ·  <span class="px-1" dir="ltr">60</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/next-js/408709573/8-0-0-dynamic-import-causing-you-may-need-an-appropriate">8.0.0 - dynamic import causing 'You may need an appropriate loader...' error</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars3.githubusercontent.com/u/2369590?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="dunika picture"> <strong class="pr-1" dir="ltr">dunika</strong>  ·  <span class="px-1" dir="ltr">58</span><span>Comments</span> </div> <hr /> <div> <strong> <a href="/next-js/361965450/trailing-slash-in-link-for-legit-page-works-for-client-side">trailing slash in link for legit page works for client side navigation but leads to not found bundle and 404 on hard refresh (ssr)</a> </strong> </div> <div class="text-muted text-small mt-2"> <img src="https://avatars1.githubusercontent.com/u/559321?v=4&s=40" style="width:20px; height:20px;" class="mr-2 rounded float-left" alt="iamstarkov picture"> <strong class="pr-1" dir="ltr">iamstarkov</strong>  ·  <span class="px-1" dir="ltr">119</span><span>Comments</span> </div> </div> </div> <div class="sticky-top pt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div id="ph-below-related-2" class="mt-4"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="3919948963" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <div class="col-12 col-lg-4"> </div> </div> <div class="skyscraper-container"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3835332123789605" data-ad-slot="7879185320" data-ad-format="vertical" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="mt-5 spacer"></div> <footer class="mt-5 pb-2 py-4 text-center mt-auto"> <div class="container"> <a class="navbar-brand logo mr-5" href="/"> <img src="/assets/img/logo.svg" width="40" height="40" alt="bleepingcoder logo"> bleeping<strong>coder</strong> </a> <div class="mt-4"> bleepingcoder.com uses publicly licensed GitHub information to provide developers around the world with solutions to their problems. We are not affiliated with GitHub, Inc. or with any developers who use GitHub for their projects. We do not host any of the videos or images on our servers. All rights belong to their respective owners. </div> <div> Source for this page: <a href="https://www.github.com/vercel/next.js/issues/3527" rel="nofollow noreferrer" target="_blank">Source</a> </div> </div> <hr class="mb-5 mt-5"> <div class="container"> <div class="row"> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular programming languages</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/python" dir="ltr">Python</a> </li> <li class="mb-2"> <a href="/javascript" dir="ltr">JavaScript</a> </li> <li class="mb-2"> <a href="/typescript" dir="ltr">TypeScript</a> </li> <li class="mb-2"> <a href="/cpp" dir="ltr">C++</a> </li> <li class="mb-2"> <a href="/csharp" dir="ltr">C#</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-sm-0 mb-5"> <strong>Popular GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/microsoft/vscode" dir="ltr">vscode</a> </li> <li class="mb-2"> <a href="/numpy/numpy" dir="ltr">numpy</a> </li> <li class="mb-2"> <a href="/ant-design/ant-design" dir="ltr">ant-design</a> </li> <li class="mb-2"> <a href="/mui-org/material-ui" dir="ltr">material-ui</a> </li> <li class="mb-2"> <a href="/vercel/next-js" dir="ltr">next.js</a> </li> </ul> </div> <div class="col-sm-4 col-lg mb-0"> <strong>More GitHub projects</strong> <ul class="list-unstyled mb-0 mt-2"> <li class="mb-2"> <a href="/rust-lang/rust" dir="ltr">rust</a> </li> <li class="mb-2"> <a href="/moment/moment" dir="ltr">moment</a> </li> <li class="mb-2"> <a href="/yarnpkg/yarn" dir="ltr">yarn</a> </li> <li class="mb-2"> <a href="/mozilla/pdf-js" dir="ltr">pdf.js</a> </li> <li class="mb-2"> <a href="/JuliaLang/julia" dir="ltr">julia</a> </li> </ul> </div> </div> </div> <hr class="mb-5 mt-5"> <div class="container text-muted"> © 2025 bleepingcoder.com - <a href="/bleeps" rel="nofollow">Contact</a><br /> By using our site, you acknowledge that you have read and understand our <a href="/cookies" rel="nofollow">Cookie Policy</a> and <a href="/privacy" rel="nofollow">Privacy Policy</a>. </div> </footer> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script> <script async src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <!--<script defer type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5fb2db66acbd74b2"></script>--> <script type="text/javascript" src="/assets/js/main.js"></script> <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script></body> </html> <script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>