next.js: Duplicate if the custom one is defined in _document.js

Bug report

I recently upgraded to the latest canary and noticed horizontal scrolling on some mobile devices. As it turned out, this was caused by #6754 (cc @developit).

Although the default new value of viewport is inserted only when there is no custom one, this logic did not work for me, because my custom viewport tag was in _document.js and not inside <Head />.

<meta name="viewport" content="width=device-width" />

Moving the tag into <Head /> solved the issue, but it took a while to investigate.

Expected behavior

It’d be great if Next.js searched for <meta name="viewport" content="..." /> inside custom _document.js and either did not insert the default one or asked developers to move their tag into <Head />. WDYT folks?

System information

  • Version of Next.js: 8.0.5-canary.17

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 24
  • Comments: 25 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Our team ran into this problem as well and ended up moving our <meta viewport...> tag from _document.js and into _app.js. In _app.js, we are importing Head from 'next/head'; which seems to remove the duplicate, whereas _document.js is importing { Head } from 'next/document';, which did not remove the duplicate.
_app.js seems like an appropriate place to put a global <meta viewport...> tag since it’s not SSR related anyway.

I would like to confirm the Expected behavior so that I can solve this one as soon as possible

  • If there is a viewport in _document don’t add the default one
  • If there is a viewport in _document and another in a page and they don’t have the same key value add both of them
  • If there is a viewport in _document and another in a page and they do have the same key value add the one in the page

Hi! After careful review we’ve decided this is the intended behavior. Head elements that are expected to be deduped should be added using next/head. The reason this surfaced is because we started defaulting a more commonly used <meta> tag – not that something is working unexpectedly.


The correct solution in this scenario is to move to using _app over _document.

In my case, whenever i use “key” property on <meta name=“viewport” … />, unwanted duplicated meta tag appears like below. even in _app.js

<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" class="next-head">

key is needed if you have a child component rewriting the father’s component “head”.

_app: <meta key="meta_description" name="description" content="general description"/>

ChildPage: <meta key="meta_description" name="description" content="page description"/>

It might help to mention this in the documentation https://nextjs.org/docs#custom-document

Head elements that are expected to be deduped should be added using next/head

A downside of this approach is whenever you need some npm package to build global <meta>s which are always the same (e.g. mobile-detect package to add <meta name="viewport"> conditionally) you’re forced to include it on both server and client side. Even though the package is actually needed only once in _document on server side (as this type of <meta>s don’t need to be re-rendered in _app).

@cheeki

In my case, whenever i use “key” property on <meta name=“viewport” … />, unwanted duplicated meta tag appears like below. even in _app.js

<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" class="next-head">

I think that it’s the right behavior check @vzaidman https://github.com/zeit/next.js/issues/6919#issuecomment-487374749 so if you want to overwrite the default meta you shouldn’t use the key

@cheeki I made a PR to fix that but logically there is no need to use the key property on meta tags (But I am not sure, maybe there is a need because I remember that like there was a situation in which key is necessary)