Render Meta Tags on The Server & Client for ReactJS

Render Meta Tags on The Server & Client for ReactJS

Render meta tags on the server & client for ReactJS. You can render meta tags with any kind of attributes / properties.

React Doc Meta

Render meta tags on the server & client for ReactJS. You can render meta tags with any kind of attributes / properties.

Built with React Side Effect.



npm install --save react-doc-meta

Dependencies: React >= 0.12.0


var App = React.createClass({
  render: function () {
    var tags = [
      {name: "description", content: "lorem ipsum dolor"},
      {itemProp: "name", content: "The Name or Title Here"},
      {itemProp: "description", content: "This is the page description"},
      {itemProp: "image", content: ""},
      {name: "twitter:card", content: "product"},
      {name: "twitter:site", content: "@publisher_handle"},
      {name: "twitter:title", content: "Page Title"},
      {name: "twitter:description", content: "Page description less than 200 characters"},
      {name: "twitter:creator", content: "@author_handle"},
      {name: "twitter:image", content: ""},
      {name: "twitter:data1", content: "$3"},
      {name: "twitter:label1", content: "Price"},
      {name: "twitter:data2", content: "Black"},
      {name: "twitter:label2", content: "Color"},
      {property: "og:title", content: "Title Here"},
      {property: "og:type", content: "article"},
      {property: "og:url", content: ""},
      {property: "og:image", content: ""},
      {property: "og:description", content: "Description Here"},
      {property: "og:site_name", content: "Site Name, i.e. Moz"},
      {property: "og:price:amount", content: "15.00"},
      {property: "og:price:currency", content: "USD"},
      {weirdfield: "something", content: "really really cool", hello:"world", meh: "hahaha"}

    // DocMeta will construct meta tags with properties & values mirroring the above key-value pairs
    return (
      <DocMeta tags={tags}>
        <this.props.activeRouteHandler />

On the browser the above will produce:

<meta name="description" content="lorem ipsum dolor" data-doc-meta="true">
<meta itemprop="name" content="The Name or Title Here" data-doc-meta="true">
<meta itemprop="description" content="This is the page description" data-doc-meta="true">
<meta itemprop="image" content="" data-doc-meta="true">
<meta name="twitter:card" content="product" data-doc-meta="true">
<meta name="twitter:site" content="@publisher_handle" data-doc-meta="true">
<meta name="twitter:title" content="Page Title" data-doc-meta="true">
<meta name="twitter:description" content="Page description less than 200 characters" data-doc-meta="true">
<meta name="twitter:creator" content="@author_handle" data-doc-meta="true">
<meta name="twitter:image" content="" data-doc-meta="true">
<meta name="twitter:data1" content="$3" data-doc-meta="true">
<meta name="twitter:label1" content="Price" data-doc-meta="true">
<meta name="twitter:data2" content="Black" data-doc-meta="true">
<meta name="twitter:label2" content="Color" data-doc-meta="true">
<meta property="og:title" content="Title Here" data-doc-meta="true">
<meta property="og:type" content="article" data-doc-meta="true">
<meta property="og:url" content="" data-doc-meta="true">
<meta property="og:image" content="" data-doc-meta="true">
<meta property="og:description" content="Description Here" data-doc-meta="true">
<meta property="og:site_name" content="Site Name, i.e. Moz" data-doc-meta="true">
<meta property="og:price:amount" content="15.00" data-doc-meta="true">
<meta property="og:price:currency" content="USD" data-doc-meta="true">
<meta weirdfield="something" content="really really cool" hello="world" meh="hahaha" data-doc-meta="true">

Works for nested components too!

Also, you don't need to wrap components with <DocMeta tags={tags}>...</DocMeta>, writing <DocMeta tags={tags} /> should work just fine.

class JoinPage extends Component {
  static propTypes = {
    status: PropTypes.string,
    user: PropTypes.object

  render() {
    var tags = [
      {name: "description", content: "test"}

    // only this meta should render in the DOM
    var tags2 = [
      {name: "description", content: "test 2"}

    return (
        <DocMeta tags={tags} />
        <DocMeta tags={tags2} />
        <JoinForm {...this.props} />

class JoinForm extends Component {

  constructor(props) {
    this.state = {

    var tags = [
      {name: "description", content: "a nested doc meta"}

    return (
        <DocMeta tags={tags} />
        <NavLink routeName="account">Test</NavLink>

On the browser the above will produce

<meta name="description" content="a nested doc meta" data-doc-meta="true">

Server Usage

If you use it on server, call DocMeta.rewind() after rendering components to string to retrieve the array of meta tags given to the innermost DocMeta. You can then embed this meta tags into HTML page template like this using es6 spread props:

  //... the rest of the html-document.jsx...

  render() {
    const { state, markup, script, css, lang } = this.props;
    let metaTags = DocMeta.rewind();

    return (
      <html >
  , index) =>
              <meta data-doc-meta="true" key={index} {...tag} />)

          <div id="root" dangerouslySetInnerHTML={{__html: markup}} />

          <script dangerouslySetInnerHTML={{__html: state}} />

Because this component keeps track of mounted instances, you have to make sure to call rewind on server, or you'll get a memory leak.


  1. TEST!

  2. Better diffing for meta tag changes on the client side


Contributions welcomed! Raise issues / submit pull requests thank you!

Release History

  • 0.1.0 Adapted solution for react-document-title for meta tags

  • 0.1.1 Removed undeclared underscore dependency. Using native array prototype functions instead.

  • 0.2.0 Merged a critical fix that causes memory leaks on _serverMeta

Download Details:

Author: geekyme

Source Code:

reactjs react javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Increase Performance of React Applications Via Array JavaScript Methods

Increase Performance of React Applications Via Array JavaScript Methods. We will create a simple event management application in the react to add, update, and delete an event.

Routing in React without React-Router

I have been using React JS in my projects for quite some time now and am used to managing routing in my app using the react-router package. I have always been keen on having as little dependencies in my apps as possible, so, I always felt perturbed by the use of this particular package in simpler apps which did not have complex routes.

The Ugly Side of React Hooks

In this post, I will share my own point of view about React Hooks, and as the title of this post implies, I am not a big fan.

ReactJS: The Javascript Developer’s Guide

This article will walk you through the concepts you would need to know to step into the world of widely used ReactJS.