next.js: Next 13 - error multiples / on tag
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_64 Binaries: Node: 16.13.1 npm: 8.19.2 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.0.0 eslint-config-next: 13.0.0 react: 18.2.0 react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next dev
Describe the Bug
When a create files
head.js :
export default async function Head({ params }) {
return (
<>
<title>Title head</title>
<meta name="description" content="Generated by head" />
</>
);
}
layout.js :
export default async function RootLayout({ children }) {
return (
<html lang="en">
<head>
<title>Title layout</title>
<meta name="description" content="Generated by layout" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</head>
<body>{children}</body>
</html>
);
}
Tags < title > and < meta > are not deleted, just add another tags in html structure :
I tried with async function and normal functions,
Thank’s
Expected Behavior
<title> and <meta> tag replacementLink to reproduction
https://github.com/john-dufrene-dev/test-nextjs13
To Reproduce
npm run dev with this repository
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 26 (5 by maintainers)
Dynamic header title & meta tags on NextJs 13
My solution (without duplicates headers):
I am faced with the problem of changing title and meta tags on the new version of NextJs 13
After a couple of hours, I found an elegant solution…
As it turned out, it is not necessary to use the head
file.tsx
Therefore, we can easily create an empty
head.tsx
in the root of the/app
folder:And then, in places where it is necessary to prescribe the usual
<head/>
and<meta/>
from the HTML markup:P.S. You can create a separate component called
AppHead.tsx
and add it where necessary by specifying parameters, for example:P.P.S. If you are superman, then you can create a
HeadProvider.tsx
component and not write<AppHead/>
in each file.Example adding head context to your web application:
See gist code: https://gist.github.com/MakStashkevich/23e8059f3b018a3c6e8e811b1a2d59b9
I hope it will be useful. ❤️
Updated today to NextJS 13.0.3, but the issue is still present, all my schema.org meta tags are moved from the body to the page head creating a mess of multiple titles and descriptions on the posts list page.
komzweb, I’m doing more/less the same now, but previously I simply accessed the post title in layout via
children.props.title
It was a much more convenient way instead of creating a separate head file for each post. I’m not using API for posts (they are simply stored in js files, because of better SEO optimization possibilities, which are not available when JSON/markdown is in use). Would be nice if someone from dev tell here how to access children’s post title from the layout, if possible. As from the docs passing props and vice versa is not possible.Should tags that are commonly used on all pages (eg favicon) also be moved to
head.js
? I have the following method working fine for now:MakStashkevich, now try to add any meta tag inside body, for example
And check if you have two titles in the head.
The default template has been fixed in https://github.com/vercel/next.js/pull/42357