nfl / react-helmet

A document head manager for React
MIT License
17.34k stars 660 forks source link

meta tags not showing on server side #632

Closed daniloab closed 3 years ago

daniloab commented 3 years ago

Do you want to request a feature or report a bug? Bug

What is the current behavior? I have a component wrapped by helmet like the example below:

const getMetaOGTags = () => {
    return `
    <Helmet>
      <meta property="og:title" content="og title">
      <meta property="og:description" content="og description">
      <meta property="og:image" content="og image">
      <meta property="og:url" content="og url">
      <meta property="og:site_name" content="og site">
      <meta property="og:locale" content="pt_BR">
      <meta property="og:type" content="website">
    </Helmet>
  `;
  };

  return (
    <Helmet>
      <title>React Helmet Test</title>
      {getMetaOGTags()}
    </Helmet>
  );

On the server side, after the ReactDOMServer.renderToString I'm calling the helmet.renderStatic() and trying to render my meta tags.

The problem is that only the title tag is rendered. The meta tags do not work.

const helmet = Helmet.renderStatic();
  return `
    <!doctype html>
      <html lang="">
      <head>
          <meta charset="utf-8" />
          ${helmet.title.toString()}
          <meta name="viewport" content="width=device-width, initial-scale=1">
          ${helmet.meta.toString()}
          ${helmet.link.toString()}
      </head>
      <body ${helmet.bodyAttributes.toString()}>
        <div id="root">${html}</div>
        <script>
          window.__initialProps__ = ${serialize(initialProps, {
            isJSON: true,
          })};
        </script>
      </body>
    </html>
  `;

result of render Screen Shot 2021-01-26 at 17 35 05

I add some console.logs and the result is:

Screen Shot 2021-01-26 at 17 32 57

What is the expected behavior? I want to render the meta tags on my server side correctly

**Which versions of React and react-helmet, and which browser / OS are affected by this issue?

daniloab commented 3 years ago

I was passing a string instead a jsx on my getMetaOGTags