React Helmet - 图1

React Helmet

npm Version Build Status Dependency Status PRs Welcome

This reusable React component will manage all of your changes to the document head with support for document title, meta, link, style, script, noscript, and base tags.

Inspired by react-document-title

Table of Contents generated with DocToc

Examples

  1. import React from "react";
  2. import Helmet from "react-helmet";
  3. export default function Application () {
  4. return (
  5. <div className="application">
  6. <Helmet title="My Title" />
  7. ...
  8. </div>
  9. );
  10. };
  1. import React from "react";
  2. import Helmet from "react-helmet";
  3. export default function Application () {
  4. return (
  5. <div className="application">
  6. <Helmet
  7. htmlAttributes={{lang: "en", amp: undefined}} // amp takes no value
  8. title="My Title"
  9. titleTemplate="MySite.com - %s"
  10. defaultTitle="My Default Title"
  11. titleAttributes={{itemprop: "name", lang: "en"}}
  12. base={{target: "_blank", href: "http://mysite.com/"}}
  13. meta={[
  14. {name: "description", content: "Helmet application"},
  15. {property: "og:type", content: "article"}
  16. ]}
  17. link={[
  18. {rel: "canonical", href: "http://mysite.com/example"},
  19. {rel: "apple-touch-icon", href: "http://mysite.com/img/apple-touch-icon-57x57.png"},
  20. {rel: "apple-touch-icon", sizes: "72x72", href: "http://mysite.com/img/apple-touch-icon-72x72.png"}
  21. ]}
  22. script={[
  23. {src: "http://include.com/pathtojs.js", type: "text/javascript"},
  24. {type: "application/ld+json", innerHTML: `{ "@context": "http://schema.org" }`}
  25. ]}
  26. noscript={[
  27. {innerHTML: `<link rel="stylesheet" type="text/css" href="foo.css" />`}
  28. ]}
  29. style={[
  30. {type: "text/css", cssText: "body {background-color: blue;} p {font-size: 12px;}"}
  31. ]}
  32. onChangeClientState={(newState) => console.log(newState)}
  33. />
  34. ...
  35. </div>
  36. );
  37. };

Features

  • Supports title, base, meta, link, script, noscript, and style tags.
  • Attributes for html and title tags.
  • Supports isomorphic/universal environment.
  • Nested components override duplicate head changes.
  • Duplicate head changes preserved when specified in same component (support for tags like “apple-touch-icon”).
  • Callback for tracking DOM changes.

Installation

  1. npm install --save react-helmet

Server Usage

To use on the server, call rewind() 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 rewind on server, or you’ll get a memory leak.

  1. ReactDOMServer.renderToString(<Handler />);
  2. let head = Helmet.rewind();
  3. head.htmlAttributes
  4. head.title
  5. head.base
  6. head.meta
  7. head.link
  8. head.script
  9. head.style

head contains the following properties:

  • htmlAttributes
  • title
  • base
  • meta
  • link
  • script
  • noscript
  • style

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

As string output

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

As React components

  1. function HTML () {
  2. const attrs = head.htmlAttributes.toComponent();
  3. return (
  4. <html {...attrs}>
  5. <head>
  6. {head.title.toComponent()}
  7. {head.meta.toComponent()}
  8. {head.link.toComponent()}
  9. </head>
  10. <body>
  11. <div id="content">
  12. // React stuff here
  13. </div>
  14. </body>
  15. </html>
  16. );
  17. }

Use Cases

  1. Nested or latter components will override duplicate changes.

    1. <Helmet
    2. title="My Title"
    3. meta={[
    4. {"name": "description", "content": "Helmet application"}
    5. ]}
    6. />
    7. <Helmet
    8. title="Nested Title"
    9. meta={[
    10. {"name": "description", "content": "Nested component"}
    11. ]}
    12. />

    Yields:

    1. <head>
    2. <title>Nested Title</title>
    3. <meta name="description" content="Nested component">
    4. </head>
  2. Use a titleTemplate to format title text in your page title

    1. <Helmet
    2. title="My Title"
    3. titleTemplate="%s | MyAwesomeWebsite.com"
    4. />
    5. <Helmet
    6. title="Nested Title"
    7. />

    Yields:

    1. <head>
    2. <title>Nested Title | MyAwesomeWebsite.com</title>
    3. </head>
  3. Duplicate meta and/or link tags in the same component are preserved

    1. <Helmet
    2. link={[
    3. {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-57x57.png"},
    4. {"rel": "apple-touch-icon", "sizes": "72x72", "href": "http://mysite.com/img/apple-touch-icon-72x72.png"}
    5. ]}
    6. />

    Yields:

    1. <head>
    2. <link rel="apple-touch-icon" href="http://mysite.com/img/apple-touch-icon-57x57.png">
    3. <link rel="apple-touch-icon" sizes="72x72" href="http://mysite.com/img/apple-touch-icon-72x72.png">
    4. </head>
  4. Duplicate tags can still be overwritten

    1. <Helmet
    2. link={[
    3. {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-57x57.png"},
    4. {"rel": "apple-touch-icon", "sizes": "72x72", "href": "http://mysite.com/img/apple-touch-icon-72x72.png"}
    5. ]}
    6. />
    7. <Helmet
    8. link={[
    9. {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-180x180.png"}
    10. ]}
    11. />

    Yields:

    1. <head>
    2. <link rel="apple-touch-icon" href="http://mysite.com/img/apple-touch-icon-180x180.png">
    3. </head>
  5. Only one base tag is allowed

    1. <Helmet
    2. base={{"href": "http://mysite.com/"}}
    3. />
    4. <Helmet
    5. base={{"href": "http://mysite.com/blog"}}
    6. />

    Yields:

    1. <head>
    2. <base href="http://mysite.com/blog">
    3. </head>
  6. defaultTitle will be used as a fallback when the template does not want to be used in the current Helmet

    1. <Helmet
    2. defaultTitle="My Site"
    3. titleTemplate="My Site - %s"
    4. />

    Yields:

    1. <head>
    2. <title>My Site</title>
    3. </head>

    But a child route with a title will use the titleTemplate, giving users a way to declare a titleTemplate for their app, but not have it apply to the root.

    1. <Helmet
    2. defaultTitle="My Site"
    3. titleTemplate="My Site - %s"
    4. />
    5. <Helmet
    6. title="Nested Title"
    7. />

    Yields:

    1. <head>
    2. <title>My Site - Nested Title</title>
    3. </head>

    And other child route components without a Helmet will inherit the defaultTitle.

  7. Usage with <script> tags:

    1. <Helmet
    2. script={[{
    3. "type": "application/ld+json",
    4. "innerHTML": `{
    5. "@context": "http://schema.org",
    6. "@type": "NewsArticle"
    7. }`
    8. }]}
    9. />

    Yields:

    1. <head>
    2. <script type="application/ld+json">
    3. {
    4. "@context": "http://schema.org",
    5. "@type": "NewsArticle"
    6. }
    7. </script>
    8. </head>
  8. Usage with <style> tags:

    1. <Helmet
    2. style={[{
    3. "cssText": `
    4. body {
    5. background-color: green;
    6. }
    7. `
    8. }]}
    9. />

    Yields:

    1. <head>
    2. <style>
    3. body {
    4. background-color: green;
    5. }
    6. </style>
    7. </head>

Contributing to this project

Please take a moment to review the guidelines for contributing.

License

MIT

More Examples

react-helmet-example