Next.js: Escaping problem with meta tags

Created on 18 May 2017  路  23Comments  路  Source: vercel/next.js

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&#x27;s it!" />

How can I handle with this? I couldn't find any solution about this problem.

Thanks.

bug upstream

Most helpful comment

I'm facing the same issue. Are there any updates on it?

All 23 comments

@Balamir could you give us some code for this?
Where we can work on locally.

Please re-open this issue after that.

@arunoda thanks for the reply.

I just tried this with your head-elements example.

Simply I added above meta:

import Head from 'next/head'

export default () => (
  <div>
    <Head>
      <title>This page has a title 馃</title>
      <meta charSet='utf-8' />
      <meta name="description" content="That's it!" />
      <meta name='viewport' content='initial-scale=1.0, width=device-width' />
    </Head>

    <h1>This page has a title 馃</h1>
  </div>
)

and the result:

snip20170519_2

Thanks.

Thanks. I'll see what's happening here.

@arunoda I couldn't reopen this issue. It's still closed. Can you re-open this issue please?

Thanks.

This Issue still exist .For single quote it is rendering as &#x27;

clothing,men's shoes,men's accessories,women's clothing,women's shoes,jewelry,women's handbags,women's accessories,dogs,furniture,home and decor,kitchen and dining

meta

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&amp;bar=baz" class="next-head"/>.

Notice the encoded &amp;. Any way around that?

I'm facing the same issue. Are there any updates on it?

I'm expecting this is an upstream React SSR issue, since we don't escape values afaik.

Running through the same issue. any workaround please ?

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!

Please track https://github.com/facebook/react/issues/13838. I'm using the following workaround:

import Entities from 'html-entities/lib/html5-entities'

const entities = new Entities()
const escapeRegExp = /(content|href|src|srcSet)="([^"]+)"/g
const escapeHandle = (match, attribute, value) => {
  return `${attribute}="${entities.decode(value)}"`
}

// ...

html = html.replace(escapeRegExp, escapeHandle)

Still have the same issue...

@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.

@timneutkens After 2 years it's normal to see people saying that the issue still there.

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.

React Helmet has a encodeSpecialCharacters boolean prop to disable the encode server side. Can it be done on next-head too?

Seems like Helmet completely bypasses React, which doesn't seem like a good idea imo.

Closing this as it's tracked on React: https://github.com/facebook/react/issues/13838

We are migration the application to next.js we have been using helmet and I tried to migrated to next/head but I found the error with for example the http://schema.org data for example to use

<script type="application/ld+json">
  {"@context":"http://schema.org","@type":"BreadcrumbList" ...
</script>

next/head convert it to, but with react-helmet return the expect value

<script type="application/ld+json">
  &quot;@context&quot;: &quot;http://schema.org&quot;,
  &quot;@type&quot;: &quot;SportsEvent&quot;,
</script>

@vxcamiloxv Have you tried with making it a string?

Like:

<script type="application/ld+json">{`
  {"@context":"http://schema.org","@type":"BreadcrumbList" ...
`}</script>

Can't remember, but think I made that work once :)

@DesignMonkey yes, my final solution was <script ype="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(json) }} />

@vxcamiloxv Oh yea sure... Maybe that was the solution I ended up with as well :)

@vxcamiloxv Thank you for sharing your solution!
I have used it to get the proper behaviour:

const ldJSON = `<JSON goes here>` 
....
and then 
in return (<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: ldJSON }}/>)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

flybayer picture flybayer  路  3Comments

olifante picture olifante  路  3Comments

kenji4569 picture kenji4569  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

jesselee34 picture jesselee34  路  3Comments