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

next/head unsetting _document <title>

Open IanMitchell opened this issue 8 years ago • 16 comments

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

IanMitchell avatar Jan 02 '18 18:01 IanMitchell

Same issue, running 4.2.1

gsimone avatar Jan 08 '18 20:01 gsimone

I had this problem with 4.3.0-canary.1

It has been corrected as of 4.4.0-canary.3

gihrig avatar Jan 14 '18 21:01 gihrig

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 avatar Jan 16 '18 10:01 timneutkens

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

Sekhmet avatar Feb 13 '18 00:02 Sekhmet

@Sekhmet sure 👍 Please add tests for it 👍

timneutkens avatar Feb 13 '18 11:02 timneutkens

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

remy avatar Mar 17 '18 07:03 remy

This is quite critical and still present. Any news?

mzzfederico avatar Mar 19 '18 16:03 mzzfederico

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

felipekm avatar Mar 20 '18 20:03 felipekm

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

kaseonedge avatar Apr 19 '18 00:04 kaseonedge

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> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/2423322?u=64f5d1eda282ff30fd54d0de58e609908b39e820&v=4" alt="ewagstaff avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 10 '18 19:05 </span> <a class="owner-name" href="http://gitmemories.com/ewagstaff"> ewagstaff </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <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> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/15183881?u=c7b27c094824996091d60c784de0197a727febd0&v=4" alt="kaseonedge avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 17 '18 03:05 </span> <a class="owner-name" href="http://gitmemories.com/kaseonedge"> kaseonedge </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <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> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/20288136?u=b1d7699783365aceb8176fb02240ed6c6aa3f301&v=4" alt="wdifruscio avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 17 '18 18:05 </span> <a class="owner-name" href="http://gitmemories.com/wdifruscio"> wdifruscio </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <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> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/2423322?u=64f5d1eda282ff30fd54d0de58e609908b39e820&v=4" alt="ewagstaff avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 24 '18 20:05 </span> <a class="owner-name" href="http://gitmemories.com/ewagstaff"> ewagstaff </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <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> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/15183881?u=c7b27c094824996091d60c784de0197a727febd0&v=4" alt="kaseonedge avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> May 24 '18 21:05 </span> <a class="owner-name" href="http://gitmemories.com/kaseonedge"> kaseonedge </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>See my comments here: https://github.com/zeit/next.js/pull/3795</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"> May 25 '18 17:05 </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>Closing this in favor of #4596 based on my comments in #3795</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"> Jun 13 '18 12:06 </span> <a class="owner-name" href="http://gitmemories.com/timneutkens"> timneutkens </a> </div> </div> <div class="clear-both"></div> </div> </section> </div> </div> </div> <aside class="detail-info-box"> <h3 class="title pkg-infobox-metadata">Labels</h3> <script type="text/javascript"> atOptions = { 'key' : '6ca19c2b16e3f20a28885886ef99a68e', 'format' : 'iframe', 'height' : 250, 'width' : 300, 'params' : {} }; </script> <script type="text/javascript" src="https://www.highperformanceformat.com/6ca19c2b16e3f20a28885886ef99a68e/invoke.js" ></script> <h3 class="title">Owner</h3> <div class="owner-info flex-items-center d-flex"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/603872?v=4" alt="IanMitchell avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <a class="owner-name" href="http://gitmemories.com/IanMitchell"> IanMitchell </a> </div> <hr class="color1"> <h3>Other Repo Issues</h3> <div class="packages"> </div> <div style="clear: both"></div> </aside> </div> </div> </main> <footer class="site-footer"> <span>© 2022 Git Memory </span> <a class="link sep" href="http://gitmemories.com/privacy-policy">Policy</a> <a class="link sep" href="http://gitmemories.com/terms">Terms </a> <a class="link sep" href="http://gitmemories.com/contact">Contact </a> <a class="link sep" href="https://exchangetuts.com">Exchangetuts </a> <a class="link sep" href="https://onltools.com/">Onltools </a> <a class="link sep" href="https://serveraide.com/">Serveraide </a> </footer> <script src="http://gitmemories.com/js/app.js?id=a1337f444c840fae34ad"></script> </body> </html>