{"_id":"@catfishkrenzer/react-meta-tags","name":"@catfishkrenzer/react-meta-tags","dist-tags":{"latest":"2.0.0"},"versions":{"2.0.0":{"name":"@catfishkrenzer/react-meta-tags","description":"Handle document meta/head tags in isomorphic react with ease.","version":"2.0.0","main":"lib/index.js","license":"MIT","sideEffects":false,"repository":{"type":"git","url":"git+https://github.com/CatfishKrenzer/react-meta-tags.git"},"bugs":{"url":"https://github.com/CatfishKrenzer/react-meta-tags/issues"},"keywords":["react-component","tags","head","meta","title","react","base","react-meta-tags"],"scripts":{"start":"cross-env npm run start-dev | nodemon example.js","start-dev":"cross-env webpack-dev-server --content-base example/ --config webpack.dev.config.js --watch-poll --progress --inline --hot","bundle":"cross-env NODE_ENV=production rollup -c rollup.config.js && npm run compile","compile":"cross-env babel src --out-dir lib"},"dependencies":{"@babel/cli":"^7.23.9","@babel/core":"^7.24.0","@babel/plugin-proposal-class-properties":"^7.18.6","@babel/plugin-proposal-object-rest-spread":"^7.20.7","@babel/preset-env":"^7.24.0","@babel/preset-react":"^7.23.3","@babel/register":"^7.23.7","babel-eslint":"^10.1.0","babel-loader":"^9.1.3","babel-plugin-add-module-exports":"^1.0.4","classnames":"^2.5.1","cross-env":"^7.0.3","eslint":"^7.32.0","eslint-config-prettier":"^9.1.0","eslint-loader":"^4.0.2","eslint-plugin-import":"^2.29.1","eslint-plugin-react":"^7.34.0","nodemon":"^3.1.0","prettier":"^3.2.5","react":"^18.2.0","react-dom":"^18.2.0","react-router":"^3.2.6","react-transform-hmr":"^1.0.4","rollup":"^1.32.1","rollup-plugin-babel":"^4.4.0","rollup-plugin-commonjs":"^10.1.0","rollup-plugin-filesize":"^10.0.0","rollup-plugin-license":"^3.2.0","rollup-plugin-node-resolve":"^5.2.0","rollup-plugin-replace":"^2.2.0","rollup-plugin-uglify":"^6.0.4","webpack":"^5.90.3","webpack-cli":"^5.1.4","webpack-dev-server":"^5.0.2"},"homepage":"https://github.com/CatfishKrenzer/react-meta-tags#readme","directories":{"example":"examples","lib":"lib"},"devDependencies":{},"gitHead":"e3f9cf79a6f9e7994c307ec5ff06ac12e9fa5f55","_id":"@catfishkrenzer/react-meta-tags@2.0.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"integrity":"sha512-QmRmwm72hzbbCUppHeKS5ZLXRIvP6biSoXo8Cnz48xz4BqmIIQ1zSsvMhzVazk9oPKNs03//HgJST9QHV989sA==","shasum":"4dae8e00c4ec5bdb775a50651d8d11aa344e29f6","tarball":"https://registry.npmjs.org/@catfishkrenzer/react-meta-tags/-/react-meta-tags-2.0.0.tgz","fileCount":22,"unpackedSize":70918,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCEvlTo23NPZ7cF0jDvREtVidxjG15HHQXccggvSDEAxwIgPS54u6jPsEAPm92vlN2MIGih/vmlbBmT7BgSMmUZUj0="}]},"_npmUser":{"name":"catfishkrenzer","email":"Krenzer.ce@gmail.com"},"maintainers":[{"name":"catfishkrenzer","email":"Krenzer.ce@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/react-meta-tags_2.0.0_1709908638897_0.9998302472875107"},"_hasShrinkwrap":false}},"time":{"created":"2024-03-08T14:37:18.781Z","2.0.0":"2024-03-08T14:37:19.105Z","modified":"2024-03-08T14:37:19.637Z"},"maintainers":[{"name":"catfishkrenzer","email":"Krenzer.ce@gmail.com"}],"description":"Handle document meta/head tags in isomorphic react with ease.","homepage":"https://github.com/CatfishKrenzer/react-meta-tags#readme","keywords":["react-component","tags","head","meta","title","react","base","react-meta-tags"],"repository":{"type":"git","url":"git+https://github.com/CatfishKrenzer/react-meta-tags.git"},"bugs":{"url":"https://github.com/CatfishKrenzer/react-meta-tags/issues"},"license":"MIT","readme":"# react-meta-tags\nHandle document meta/head tags in isomorphic react with ease.\n\nHandling 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 <a href=\"https://github.com/nfl/react-helmet\" target=\"_blank\">react-helmet</a> and  <a href=\"https://github.com/kodyl/react-document-meta\" target=\"_blank\">react-document-meta</a>, 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.\n\n### Install\nThrough npm\n`npm install react-meta-tags --save`\n\nOr get compiled development and production version from ./dist\n\n### Usage\n\n#### Using MetaTag Component\n\n```jsx\nimport React from 'react';\nimport MetaTags from 'react-meta-tags';\n\nclass Component1 extends React.Component {\n  render() {\n    return (\n        <div className=\"wrapper\">\n          <MetaTags>\n            <title>Page 1</title>\n            <meta name=\"description\" content=\"Some description.\" />\n            <meta property=\"og:title\" content=\"MyApp\" />\n            <meta property=\"og:image\" content=\"path/to/image.jpg\" />\n          </MetaTags>\n          <div className=\"content\"> Some Content </div>\n        </div>\n      )\n  }\n}\n```\nNote : Define id on tags so if you navigate to other page, older meta tags will be removed and replaced by new ones.\n\n\n#### ReactTitle Component\nIf you just want to add title on a page you can use ReactTitle instead.\n```jsx\nimport React from 'react';\nimport {ReactTitle} from 'react-meta-tags';\n\nclass Component2 extends React.Component {\n  render() {\n    return (\n        <div className=\"wrapper\">\n          <ReactTitle title=\"Page 2\"/>\n          <div className=\"content\"> Some Content </div>\n        </div>\n      )\n  }\n}\n```\n\n### Server Usage Example\n\n```jsx\nimport MetaTagsServer from 'react-meta-tags/server';\nimport {MetaTagsContext} from 'react-meta-tags';\n/** Import other required modules **/\n\n/*\n------\n  some serve specific code\n------\n*/\n\napp.use((req, res) => {\n  //make sure you get a new metatags instance for each request\n  const metaTagsInstance = MetaTagsServer();\n\n  //react router match\n  match({\n    routes, location: req.url\n  }, (error, redirectLocation, renderProps) => {\n    let reactString;\n\n    try{\n      reactString = ReactDomServer.renderToString(\n      <Provider store={store}> {/*** If you are using redux ***/}\n      {/* You have to pass extract method through MetaTagsContext so it can catch meta tags */}\n        <MetaTagsContext extract = {metaTagsInstance.extract}>\n          <RouterContext {...renderProps}/>\n        </MetaTagsContext>\n      </Provider>\n      );\n    }\n    catch(e){\n      res.status(500).send(e.stack);\n      return;\n    }\n\n    //get all title and metatags as string\n    const meta = metaTagsInstance.renderToString();\n\n    //append metatag string to your layout\n    const htmlStr = (`\n      <!doctype html>\n      <html lang=\"en-us\">\n        <head>\n          <meta charSet=\"utf-8\"/>\n          ${meta}\n        </head>\n        <body>\n          <div id=\"content\">\n            ${reactString}\n          </div>\n        </body>\n      </html>  \n    `);\n\n    res.status(200).send(layout);\n  });\n});\n```\n\nSo as per above code we have to do following for server rendering\n\n1. Import MetaTagsServer and MetaTagsContext\n2. Create a new instance of MetaTagsServer\n3. Wrap your component inside MetaTagsContext and pass extract method as props\n4. Extract meta string using renderToString of MetaTagsServer instance\n5. Append meta string to your html template.\n\n#### JSX Layout\nYou 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.\n```jsx\n//get all title and metatags as React elements\nconst metaTags = metaTagsInstance.getTags();\n\n//append metatag string to your layout\nconst layout = (\n  <html lang=\"en-us\">\n    <head>\n      <meta charSet=\"utf-8\"/>\n      {metaTags}\n    </head>\n    <body>\n      <div id=\"app\" dangerouslySetInnerHTML={{__html: reactString}} />\n    </body>\n  </html>  \n);\n\nconst htmlStr = ReactDomServer.renderToString(layout);\n\nres.status(200).send(htmlStr);\n```\n\n\n\n## Meta Tag Uniqueness\n- The module uniquely identifies meta tag by id / property / name / itemProp attribute.\n- 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.\n- You should give an id if meta key is different then property/name/itemProp to uniquely identify them.\n","readmeFilename":"README.md"}