What is gatsby-plugin-react-helmet?
The gatsby-plugin-react-helmet package is a Gatsby plugin that integrates React Helmet into your Gatsby site. React Helmet is a library that allows you to manage changes to the document head, such as the title, meta tags, and other elements, from within your React components.
What are gatsby-plugin-react-helmet's main functionalities?
Setting the Page Title
This feature allows you to set the title of the page dynamically from within a React component. The title tag is updated in the document head when the component is rendered.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<title>My Page Title</title>
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Adding Meta Tags
This feature allows you to add meta tags to the document head. You can specify various meta tags such as charset, description, keywords, etc., directly within your React components.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<meta charSet="utf-8" />
<meta name="description" content="This is a description of my page" />
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Link Tags
This feature allows you to add link tags to the document head. This is useful for things like setting the canonical URL of the page, adding stylesheets, or preloading resources.
import React from 'react';
import { Helmet } from 'react-helmet';
const MyComponent = () => (
<div>
<Helmet>
<link rel="canonical" href="https://www.example.com/my-page" />
</Helmet>
<h1>Hello, world!</h1>
</div>
);
export default MyComponent;
Other packages similar to gatsby-plugin-react-helmet
react-helmet
React Helmet is the core library that gatsby-plugin-react-helmet integrates with. It provides the same functionality for managing changes to the document head in any React application, not just Gatsby.
react-meta-tags
React Meta Tags is another library for managing the document head in React applications. It offers similar functionality to React Helmet but with a different API and some additional features like support for nested tags.
gatsby-plugin-react-helmet
Provides drop-in support for server rendering data added with
React Helmet.
React Helmet is a component which lets you control your document head using
their React component.
With this plugin, attributes you add in their component, e.g. title, meta
attributes, etc. will get added to the static HTML pages Gatsby builds.
This is important not just for site viewers, but also for SEO -- title and description metadata stored in the document head is a key component used by Google in determining placement in search results.
⚠️ This package will be deprecated
The gatsby-plugin-react-helmet
package will be deprecated in the future. The new Gatsby Head API is easier to use, more performant, has a smaller bundle size, and supports the latest React features. Update to gatsby@^4.19.0
to use it.
Install
npm install gatsby-plugin-react-helmet react-helmet
How to use
Just add the plugin to the plugins array in your gatsby-config.js
plugins: [`gatsby-plugin-react-helmet`]
Titles don't appear when opening in the background, while using gatsby-plugin-offline
If you're using gatsby-plugin-offline
, you might notice that when opening a link in the background, the title doesn't appear in the tab bar until switching to that tab. This is an upstream issue with React Helmet; however, it can be worked around by passing the defer={false}
prop into your Helmet
component. For example:
<Helmet title="foo bar" defer={false} />
Compatibility with React 16.8 useEffect hook
If you are using this plugin with React hooks, you may notice some errors like maximum call stack size exceeded
. To ensure everything is running smoothly when using these technologies together, make sure to validate the following:
- You have updated to the latest version of
gatsby-plugin-react-helmet
- You are using version 6.0.0-beta or later of
react-helmet
- You are importing React Helmet using
import { Helmet } from 'react-helmet'
rather than the old import Helmet from 'react-helmet'