https://github.com/s-yadav/react-meta-tags
Handle document meta/head tags in isomorphic react with ease.
https://github.com/s-yadav/react-meta-tags
Last synced: 2 days ago
JSON representation
Handle document meta/head tags in isomorphic react with ease.
- Host: GitHub
- URL: https://github.com/s-yadav/react-meta-tags
- Owner: s-yadav
- License: mit
- Created: 2016-09-21T09:10:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-25T23:35:49.000Z (about 2 years ago)
- Last Synced: 2025-04-12T23:39:39.301Z (2 days ago)
- Language: JavaScript
- Size: 903 KB
- Stars: 230
- Watchers: 3
- Forks: 46
- Open Issues: 35
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - s-yadav/react-meta-tags - Handle document meta/head tags in isomorphic react with ease. (JavaScript)
README
# react-meta-tags
Handle document meta/head tags in isomorphic react with ease.Handling title and meta/head tags in a isomporhic react is tricky. Its declarative to define those tags within the component, but they need to be moved on document head on client side as well as server side. While there are other modules which helps with the use-case like react-helmet and react-document-meta, but they require to define those tags in a object literal. react-meta-tags allow you to write those tags in a declarative way and in normal jsx format.
### Install
Through npm
`npm install react-meta-tags --save`Or get compiled development and production version from ./dist
### Usage
#### Using MetaTag Component
```jsx
import React from 'react';
import MetaTags from 'react-meta-tags';class Component1 extends React.Component {
render() {
return (
Page 1
Some Content
)
}
}
```
Note : Define id on tags so if you navigate to other page, older meta tags will be removed and replaced by new ones.#### ReactTitle Component
If you just want to add title on a page you can use ReactTitle instead.
```jsx
import React from 'react';
import {ReactTitle} from 'react-meta-tags';class Component2 extends React.Component {
render() {
return (
Some Content
)
}
}
```### Server Usage Example
```jsx
import MetaTagsServer from 'react-meta-tags/server';
import {MetaTagsContext} from 'react-meta-tags';
/** Import other required modules **//*
------
some serve specific code
------
*/app.use((req, res) => {
//make sure you get a new metatags instance for each request
const metaTagsInstance = MetaTagsServer();//react router match
match({
routes, location: req.url
}, (error, redirectLocation, renderProps) => {
let reactString;try{
reactString = ReactDomServer.renderToString(
{/*** If you are using redux ***/}
{/* You have to pass extract method through MetaTagsContext so it can catch meta tags */}
);
}
catch(e){
res.status(500).send(e.stack);
return;
}//get all title and metatags as string
const meta = metaTagsInstance.renderToString();//append metatag string to your layout
const htmlStr = (`
${meta}
${reactString}
`);res.status(200).send(layout);
});
});
```So as per above code we have to do following for server rendering
1. Import MetaTagsServer and MetaTagsContext
2. Create a new instance of MetaTagsServer
3. Wrap your component inside MetaTagsContext and pass extract method as props
4. Extract meta string using renderToString of MetaTagsServer instance
5. Append meta string to your html template.#### JSX Layout
You might also be using React to define your layout, in which case you can use `getTags` method from `metaTagsInstance`. The layout part of above server side example will look like this.
```jsx
//get all title and metatags as React elements
const metaTags = metaTagsInstance.getTags();//append metatag string to your layout
const layout = (
{metaTags}
);const htmlStr = ReactDomServer.renderToString(layout);
res.status(200).send(htmlStr);
```## Meta Tag Uniqueness
- The module uniquely identifies meta tag by id / property / name / itemProp attribute.
- Multiple meta tags with same property / name is valid in html. If you need such case. Define a different id to both so that it can be uniquely differentiate.
- You should give an id if meta key is different then property/name/itemProp to uniquely identify them.