Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use meta tags with dynamic value in react js?

Can anyone help me regarding how to use meta tags with dynamic value in react js?

Please see the image for my requirement,

code screenshot

I am using the extra metatag html tag here(because react require wrap complete html inside the single tag else it raise error). I can also use div/p any html tag, but is this right way to render the react component? having extra html tag than inside that meta tags. Will this work for SEO?

Please suggested me any other good way to use meta tags manually.

like image 544
iamsonivivek Avatar asked Aug 19 '16 07:08

iamsonivivek


People also ask

What is dynamic meta tag?

Dynamic Meta Tags are used when there is a dynamic element on the page such as a title, deck, and page body. In the example below, the fields, Profile_Name, Profile_Symbol, MetaDesc, and MetaKeywords are used to populate the title with a public company name, and exchange symbol.

How do you use metatags?

<meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings. Metadata will not be displayed on the page, but is machine parsable.


2 Answers

I can see few issues regarding the code which you shared.

  1. Meta tags come under head, but your react components would be rendered in your body tag.
  2. Considering SEO part, google can parse JS now so your tags would be read but bing and if you consider yahoo still cannot still do that( Google also is really not that efficient still, faced too many issues regarding while handling SEO's with single page app)
  3. If your reacts components uses Link to navigate to other components which I am assuming it would it case of SPA it would not work, because crawlers try to reach you page directly.

Now,if you have a single page app with a single component you can try react-helmet , but if it involves multiple components and navigations I would suggest you to go for pre-rendering,maybe using phatom-js or pre-render.io(which indirectly uses phantomjs).

If your only concern is meta tags, then you can embed you meta tags directly into your html code and not in the components. This would really help the crawlers to see the meta tags.

But,if you also want crawlers to see your content, pre-rendering is best solution which I can think of now.

like image 71
Harkirat Saluja Avatar answered Oct 27 '22 23:10

Harkirat Saluja


If you are serving your React bundle from a server, you can dynamically generate meta tags on the server.

Essentially, in your public/index.html file you want to replace the metadata with an identifiable string:

<!-- in public/index.html -->
<title>$OG_TITLE</title>
<meta name="description"        content="$OG_DESCRIPTION" />
<meta property="og:title"       content="$OG_TITLE" />
<meta property="og:description" content="$OG_DESCRIPTION" />
<meta property="og:image"       content="$OG_IMAGE" />

And then on the server, you want to replace these strings with the dynamically generated information. Here is an example route with Node and Express:

app.get('/about', function(request, response) {
  console.log('About page visited!');
  const filePath = path.resolve(__dirname, './build', 'index.html')
  fs.readFile(filePath, 'utf8', function (err,data) {
    if (err) {
      return console.log(err);
    }
    data = data.replace(/\$OG_TITLE/g, 'About Page');
    data = data.replace(/\$OG_DESCRIPTION/g, "About page description");
    result = data.replace(/\$OG_IMAGE/g, 'https://i.imgur.com/V7irMl8.png');
    response.send(result);
  });
});

Taken from this tutorial here: https://www.kapwing.com/blog/how-to-add-dynamic-meta-tags-server-side-with-create-react-app/

like image 22
justswim Avatar answered Oct 28 '22 00:10

justswim