next.js: Escaping problem with meta tags
Hi,
I’m facing an issue with meta tags. When I use some special characters like single quote in the head tag, it’s escaping. I can not use dangerouslySetInnerHtml for meta tags.
in code:
<meta name="description" content="That's it!" />
result:
<meta name="description" content="That's it!" />
How can I handle with this? I couldn’t find any solution about this problem.
Thanks.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 11
- Comments: 28 (10 by maintainers)
I’m facing the same issue. Are there any updates on it?
I’ve made a workaround by post-processing the generated HTML and using a regex for the attributes that are incorrectly encoded with HTML entities:
https://gist.github.com/stefl/1f8c246dd7ca9cb332ae41f68e80088d
Obviously not a long-term fix but it means that my Opengraph metadata is now not affected by this. Hope this is of use to others here!
React Helmet has a
encodeSpecialCharactersboolean prop to disable the encode server side. Can it be done on next-head too?@mrasoahaingo it’s not really relevant to post “same issue”, as @oliviertassinari said it’s an upstream issue. If the issue is open you can consider it’s still there an not solved, so saying “same issue” is not really useful, if you want to “+1” an issue use GitHub’s reaction system on the initial post to 👍
Thanks.
Still have the same issue…
Closing this as it’s tracked on React: https://github.com/facebook/react/issues/13838
The last comment before the “same issue” clearly said it’s a bug in React, the issue on React’s side is still open, hence why it’s not valuable to say something along the lines of “Same issue…”, it doesn’t add value, this is why the emoji reactions exist in GitHub.
@timneutkens After 2 years it’s normal to see people saying that the issue still there.
I used the Next.js
_documentfile to replace all the&occurrences. I am using some code from @stefl gist to do the replacement. The main point is to use_documentto handle this issue, so please ignore the code.My use case were images with w and h query parameters being incorrectly used by the crawlers since those pages were made with getStaticProps and they all had
&instead of&.P.S. you can move
decodeHtmlEntitiesin an util filedecodeHtmlEntities.ts, e.g.:See the images attached. First one is showing before the replace, and second one with the fix. Third one is to show also srcSet. There are also Node and Chrome logs showing how they didn’t have issue anyway.
@timneutkens is using
_documentto do this replace okay?Kudos to @isBatak for finding this out.
Found a workaround for
&in the image url in next.js — the main issue was the imgix params in the URL weren’t sticking, due to the escaped ampersands. So I took the properties out of the meta tag, and wrote a backend proxy to deliver the image data directly:So in your template:
const shareImage = myfile.jpg // without any query paramsThen in
api/share.js:Probably need to validate the incoming url, otherwise it could be open for malice, but this fixes the issue until this is merged in: https://github.com/facebook/react/issues/13838
@vxcamiloxv Thank you for sharing your solution! I have used it to get the proper behaviour:
Running through the same issue. any workaround please ?
The same is true for urls.
Adding
<meta property="og:image" content="http://example.com?foo=bar&bar=baz" />results in<meta property="og:image" content="http://example.com?foo=bar&bar=baz" class="next-head"/>.Notice the encoded
&. Any way around that?