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
- Moving viewport meta tag to _app.js For some reason duplicate was created if meta viewport was added in _document. See issue: https://github.com/zeit/next.js/issues/6919 — committed to peacefulseeker/posture-estimator by deleted user 7 years ago
- fix mobile viewport it was weird. see https://github.com/zeit/next.js/issues/6919 — committed to jeremyBanks/speedruns by jeremyBanks 7 years ago
- Update Meta.tsx to include key attibutes https://github.com/vercel/next.js/issues/6919#issuecomment-487374749 Adding key attribute to meta descriptions inside of <Head/> will stop duplicates. This ... — committed to lmuller18/keycapsets.com by lmuller18 6 years ago
Our team ran into this problem as well and ended up moving our
<meta viewport...>tag from_document.jsand into_app.js. In_app.js, we are importingHead from 'next/head';which seems to remove the duplicate, whereas_document.jsis importing{ Head } from 'next/document';, which did not remove the duplicate._app.jsseems 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
viewportin_documentdon’t add the default oneviewportin_documentand another in apageand they don’t have the samekeyvalue add both of themviewportin_documentand another in apageand they do have the samekeyvalue add the one in thepageHi! 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
_appover_document.I think I’m seeing the same issue with
https://github.com/mui-org/material-ui/blob/4516cafd6959fafccae96ca0bc70527d0fd5cf02/examples/nextjs-next/pages/_document.js#L12
In my case, whenever i use “key” property on <meta name=“viewport” … />, unwanted duplicated meta tag appears like below. even in
_app.jskey 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
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-detectpackage 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_documenton server side (as this type of<meta>s don’t need to be re-rendered in_app).@cheeki
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
keyproperty on meta tags (But I am not sure, maybe there is a need because I remember that like there was a situation in whichkeyis necessary)