React Helmet

A document head manager for React

README


React Helmet

npm Version codecov.io Build Status Dependency Status PRs Welcome

This reusable React component will manage all of your changes to the document head.

Helmet _takes_ plain HTML tags and _outputs_ plain HTML tags. It's dead simple, and React beginner friendly.



Example

  1. ``` js
  2. import React from "react";
  3. import {Helmet} from "react-helmet";

  4. class Application extends React.Component {
  5.   render () {
  6.     return (
  7.         <div className="application">
  8.             <Helmet>
  9.                 <meta charSet="utf-8" />
  10.                 <title>My Title</title>
  11.                 <link rel="canonical" href="http://mysite.com/example" />
  12.             </Helmet>
  13.             ...
  14.         </div>
  15.     );
  16.   }
  17. };
  18. ```

Nested or latter components will override duplicate changes:

  1. ``` js
  2. <Parent>
  3.     <Helmet>
  4.         <title>My Title</title>
  5.         <meta name="description" content="Helmet application" />
  6.     </Helmet>
  7. <Child>
  8.         <Helmet>
  9.             <title>Nested Title</title>
  10.             <meta name="description" content="Nested component" />
  11.         </Helmet>
  12.     </Child>
  13. </Parent>
  14. ```

outputs:

  1. ``` html
  2. <head>
  3.     <title>Nested Title</title>
  4.     <meta name="description" content="Nested component">
  5. </head>
  6. ```

See below for a full reference guide.

Features

- Supports all valid head tags: title, base, meta, link, script, noscript, and style tags.
- Supports attributes for body, html and title tags.
- Supports server-side rendering.
- Nested components override duplicate head changes.
- Duplicate head changes are preserved when specified in the same component (support for tags like "apple-touch-icon").
- Callback for tracking DOM changes.

Compatibility


Helmet 5 is fully backward-compatible with previous Helmet releases, so you can upgrade at any time without fear of breaking changes. We encourage you to update your code to our more semantic API, but please feel free to do so at your own pace.

Installation


Yarn:
  1. ``` sh
  2. yarn add react-helmet
  3. ```

npm:
  1. ``` sh
  2. npm install --save react-helmet
  3. ```

Server Usage

To use on the server, call Helmet.renderStatic() after ReactDOMServer.renderToString or ReactDOMServer.renderToStaticMarkup to get the head data for use in your prerender.

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

  1. ``` js
  2. ReactDOMServer.renderToString(<Handler />);
  3. const helmet = Helmet.renderStatic();
  4. ```

This helmet instance contains the following properties:
- base
- bodyAttributes
- htmlAttributes
- link
- meta
- noscript
- script
- style
- title

Each property contains toComponent() and toString() methods. Use whichever is appropriate for your environment. For attributes, use the JSX spread operator on the object returned by toComponent(). E.g:

As string output

  1. ``` js
  2. const html = `
  3.     doctype html>
  4.     <html ${helmet.htmlAttributes.toString()}>
  5.         <head>
  6.             ${helmet.title.toString()}
  7.             ${helmet.meta.toString()}
  8.             ${helmet.link.toString()}
  9.         </head>
  10.         <body ${helmet.bodyAttributes.toString()}>
  11.             <div id="content">
  12.                 // React stuff here
  13.             </div>
  14.         </body>
  15.     </html>
  16. `;
  17. ```

As React components

  1. ``` js
  2. function HTML () {
  3.     const htmlAttrs = helmet.htmlAttributes.toComponent();
  4.     const bodyAttrs = helmet.bodyAttributes.toComponent();

  5.     return (
  6.         <html {...htmlAttrs}>
  7.             <head>
  8.                 {helmet.title.toComponent()}
  9.                 {helmet.meta.toComponent()}
  10.                 {helmet.link.toComponent()}
  11.             </head>
  12.             <body {...bodyAttrs}>
  13.                 <div id="content">
  14.                     // React stuff here
  15.                 </div>
  16.             </body>
  17.         </html>
  18.     );
  19. }
  20. ```

Note: Use the same instance

If you are using a prebuilt compilation of your app with webpack in the server be sure to include this in the webpack file so that the same instance of react-helmet is used.
  1. ```
  2. externals: ["react-helmet"],
  3. ```
Or to import the react-helmet instance from the app on the server.

Reference Guide


  1. ``` js
  2. <Helmet
  3.     {/* (optional) set to false to disable string encoding (server-only) */}
  4.     encodeSpecialCharacters={true}

  5.     {/*
  6.         (optional) Useful when you want titles to inherit from a template:

  7.         <Helmet
  8.             titleTemplate="%s | MyAwesomeWebsite.com"
  9.         >
  10.             <title>Nested Title</title>
  11.         </Helmet>

  12.         outputs:

  13.         <head>
  14.             <title>Nested Title | MyAwesomeWebsite.com</title>
  15.         </head>
  16.     */}
  17.     titleTemplate="MySite.com - %s"

  18.     {/*
  19.         (optional) used as a fallback when a template exists but a title is not defined

  20.         <Helmet
  21.             defaultTitle="My Site"
  22.             titleTemplate="My Site - %s"
  23.         />

  24.         outputs:

  25.         <head>
  26.             <title>My Site</title>
  27.         </head>
  28.     */}
  29.     defaultTitle="My Default Title"
  30.     
  31.     {/* (optional) set to false to not use requestAnimationFrame and instead update the DOM as soon as possible.
  32.         Useful if you want to update the title when the tab is out of focus
  33.     */}
  34.     defer={false}

  35.     {/* (optional) callback that tracks DOM changes */}
  36.     onChangeClientState={(newState, addedTags, removedTags) => console.log(newState, addedTags, removedTags)}
  37. >
  38.     {/* html attributes */}
  39.     <html lang="en" amp />

  40.     {/* body attributes */}
  41.     <body className="root" />

  42.     {/* title attributes and value */}
  43.     <title itemProp="name" lang="en">My Plain Title or {`dynamic`} title</title>

  44.     {/* base element */}
  45.     <base target="_blank" href="http://mysite.com/" />

  46.     {/* multiple meta elements */}
  47.     <meta name="description" content="Helmet application" />
  48.     <meta property="og:type" content="article" />

  49.     {/* multiple link elements */}
  50.     <link rel="canonical" href="http://mysite.com/example" />
  51.     <link rel="apple-touch-icon" href="http://mysite.com/img/apple-touch-icon-57x57.png" />
  52.     <link rel="apple-touch-icon" sizes="72x72" href="http://mysite.com/img/apple-touch-icon-72x72.png" />
  53.     {locales.map((locale) => {
  54.         <link rel="alternate" href="http://example.com/{locale}" hrefLang={locale} key={locale}/>
  55.     })}

  56.     {/* multiple script elements */}
  57.     <script src="http://include.com/pathtojs.js" type="text/javascript" />

  58.     {/* inline script elements */}
  59.     <script type="application/ld+json">{`
  60.         {
  61.             "@context": "http://schema.org"
  62.         }
  63.     `}</script>

  64.     {/* noscript elements */}
  65.     <noscript>{`
  66.         <link rel="stylesheet" type="text/css" href="foo.css" />
  67.     `}</noscript>

  68.     {/* inline style elements */}
  69.     <style type="text/css">{`
  70.         body {
  71.             background-color: blue;
  72.         }

  73.         p {
  74.             font-size: 12px;
  75.         }
  76.     `}</style>
  77. </Helmet>
  78. ```

Contributing to this project

Please take a moment to review the guidelines for contributing.


License


MIT