<title> problem: A title element received an array with more than 1 element as children #38256
-
On my website, users can type a search query into a search field. I would like to set the My first question is: Does it make a difference (SEO-wise) if I set this title from a value returned from
The other approach would look like this:
And the second part of the question: No matter which approach I use, I'm getting this warning in the console:
which I didn't find any results in Google for whatsoever. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 21 replies
-
Hi, The warning is because when you do: const Home: NextPage = () => {
const world = "World";
return (
<div>
<span>Hello {world} foo</span>
</div>
);
}; React renders: <div><span>Hello <!-- -->World<!-- --> foo</span></div> That's a The 5 nodes are, Do this instead: const Home: NextPage = () => {
const world = "World";
const message = `Hello ${world} foo`;
return (
<div>
<span>{message}</span>
</div>
);
}; Which outputs: <div><span>Hello World foo</span></div> Now the SEO MeritsWell... in either case the bot/crawler, or whatever SEO entity, has to be arrive with the query in the URL already. Since you are using If you visit the site, with query in the URL, and then right click to "View Page Source", do you see the correct title there? If so, then you are good. EditThis answer has gotten a lot of feedback! I've also answered to it in a few other places but I think this one is a good complement: The key take-away from it:
For example in a codesandbox: import "./styles.css";
import { renderToString } from "react-dom/server";
const Footer = () => {
return (
<footer>
<p>{`Copyright © ${new Date().getFullYear()} Sample Company`}</p>
</footer>
);
};
// const Footer = () => {
// return (
// <footer>
// <p>Copyright © {new Date().getFullYear()} Sample Company</p>
// </footer>
// );
// };
function App() {
return (
<>
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
<Footer />
</>
);
}
console.log(renderToString(<App />)); // log to the console to see what `renderToString` outputs |
Beta Was this translation helpful? Give feedback.
-
I tried the answer here and I seem to still have the error:
I did not have such an error till after I upgraded to Next.JS v |
Beta Was this translation helpful? Give feedback.
-
This solved for me -
|
Beta Was this translation helpful? Give feedback.
-
} |
Beta Was this translation helpful? Give feedback.
-
I just happened to find this warning in my console. Is there a lint rule or some other way to catch this? |
Beta Was this translation helpful? Give feedback.
-
thank you @icyJoseph with this, it was the spaces in the
Before <title> {title} </title>
// ↑ ↑
// the whitespace is interpreted as 2 more text nodes After (fixed) <title>{title}</title> |
Beta Was this translation helpful? Give feedback.
-
Why not just doing it like this:
|
Beta Was this translation helpful? Give feedback.
-
What we are all doing is workaround, it should just accept
This is clearly a bug |
Beta Was this translation helpful? Give feedback.
-
I had the same Warning message and Solved : In my case, I had a Seo component and used the Head inside and had the title as a prop and had some text together like this :
Due to the warning message, I had changed my Seo component using template literal(``) like below and error is gone:
|
Beta Was this translation helpful? Give feedback.
Hi,
The warning is because when you do:
React renders:
That's a
div
with aspan
child, with 0 children elements, but 5 nodes as children. There's a subtle difference between elements and nodes. Here's a handy video.The 5 nodes are,
text
,comment
,text
,comment
,text
. The same is happening to yourtitle
element.Do this instead:
Which outputs: