next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Escaping problem with meta tags

Open Balamir opened this issue 9 years ago • 28 comments

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.

Balamir avatar May 18 '17 08:05 Balamir

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

Please re-open this issue after that.

arunoda avatar May 18 '17 22:05 arunoda

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

Balamir avatar May 18 '17 23:05 Balamir

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

arunoda avatar May 19 '17 00:05 arunoda

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

Thanks.

Balamir avatar May 19 '17 07:05 Balamir

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

Rupeshn73 avatar Jul 14 '17 11:07 Rupeshn73

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?

damusnet avatar Aug 04 '17 17:08 damusnet

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

poohitan avatar Jan 05 '18 16:01 poohitan

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

timneutkens avatar Jan 08 '18 09:01 timneutkens

Running through the same issue. any workaround please ?

donaminos avatar Mar 09 '18 14:03 donaminos

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!

stefl avatar May 19 '18 15:05 stefl

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)

oliviertassinari avatar Oct 12 '18 12:10 oliviertassinari

Still have the same issue...

mrasoahaingo avatar Jan 15 '19 13:01 mrasoahaingo

@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 avatar Jan 15 '19 13:01 timneutkens

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

equinusocio avatar Jul 26 '19 08:07 equinusocio

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 avatar Jul 26 '19 11:07 timneutkens

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

equinusocio avatar Jul 27 '19 10:07 equinusocio

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

timneutkens avatar Jul 27 '19 10:07 timneutkens

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

timneutkens avatar Jan 02 '20 19:01 timneutkens

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>

ghost avatar Aug 22 '20 00:08 ghost

@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 :)

morfarpeter avatar Aug 23 '20 00:08 morfarpeter