Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-meta-tags
Advanced tools
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.
Through npm
npm install react-meta-tags --save
Or get compiled development and production version from ./dist
import React from 'react';
import MetaTags from 'react-meta-tags';
class Component1 extends React.Component {
render() {
return (
<div className="wrapper">
<MetaTags>
<title>Page 1</title>
<meta name="description" content="Some description." />
<meta property="og:title" content="MyApp" />
<meta property="og:image" content="path/to/image.jpg" />
</MetaTags>
<div className="content"> Some Content </div>
</div>
)
}
}
Note : Define id on tags so if you navigate to other page, older meta tags will be removed and replaced by new ones.
If you just want to add title on a page you can use ReactTitle instead.
import React from 'react';
import {ReactTitle} from 'react-meta-tags';
class Component2 extends React.Component {
render() {
return (
<div className="wrapper">
<ReactTitle title="Page 2"/>
<div className="content"> Some Content </div>
</div>
)
}
}
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(
<Provider store={store}> {/*** If you are using redux ***/}
{/* You have to pass extract method through MetaTagsContext so it can catch meta tags */}
<MetaTagsContext extract = {metaTagsInstance.extract}>
<RouterContext {...renderProps}/>
</MetaTagsContext>
</Provider>
);
}
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 = (`
<!doctype html>
<html lang="en-us">
<head>
<meta charSet="utf-8"/>
${meta}
</head>
<body>
<div id="content">
${reactString}
</div>
</body>
</html>
`);
res.status(200).send(layout);
});
});
So as per above code we have to do following for server rendering
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.
//get all title and metatags as React elements
const metaTags = metaTagsInstance.getTags();
//append metatag string to your layout
const layout = (
<html lang="en-us">
<head>
<meta charSet="utf-8"/>
{metaTags}
</head>
<body>
<div id="app" dangerouslySetInnerHTML={{__html: reactString}} />
</body>
</html>
);
const htmlStr = ReactDomServer.renderToString(layout);
res.status(200).send(htmlStr);
FAQs
Handle document meta/head tags in isomorphic react with ease.
The npm package react-meta-tags receives a total of 19,588 weekly downloads. As such, react-meta-tags popularity was classified as popular.
We found that react-meta-tags demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.