Gatsby plugin that provides server-side rendering support for React Helmet document head management.
npx @tessl/cli install tessl/npm-gatsby-plugin-react-helmet@6.15.0Gatsby Plugin React Helmet provides server-side rendering support for React Helmet in Gatsby applications. This plugin extracts React Helmet data during the build process and injects it into the static HTML pages that Gatsby generates, ensuring proper SEO optimization and social media sharing capabilities.
⚠️ Deprecation Notice: This plugin will be deprecated in favor of Gatsby's built-in Head API. For new projects, consider using the Gatsby Head API with Gatsby v4.19.0+.
npm install gatsby-plugin-react-helmet react-helmetThis plugin works through Gatsby's plugin system and does not export functions for direct import. Instead, it automatically integrates with Gatsby's build process when added to your gatsby-config.js file.
Add the plugin to your Gatsby configuration:
// gatsby-config.js
module.exports = {
plugins: [
'gatsby-plugin-react-helmet'
]
};Then use React Helmet in your components:
import React from "react";
import { Helmet } from "react-helmet";
function MyPage() {
return (
<>
<Helmet>
<title>My Page Title</title>
<meta name="description" content="Page description for SEO" />
<meta property="og:title" content="My Page Title" />
<link rel="canonical" href="https://example.com/my-page" />
</Helmet>
<h1>My Page Content</h1>
</>
);
}
export default MyPage;The plugin operates through Gatsby's plugin hook system, implementing server-side rendering hooks that execute during the build process:
onRenderBody hook to extract React Helmet data during server-side rendering<head> section of generated HTML pagesThe plugin implements Gatsby's server-side rendering API to extract and inject React Helmet data.
/**
* Gatsby SSR API hook that processes React Helmet data during build
* @param {Object} params - Parameters object
* @param {Function} params.setHeadComponents - Function to set components in document head
* @param {Function} params.setHtmlAttributes - Function to set HTML element attributes
* @param {Function} params.setBodyAttributes - Function to set body element attributes
*/
function onRenderBody({
setHeadComponents,
setHtmlAttributes,
setBodyAttributes,
});This hook:
Helmet.renderStatic() to extract all helmet data from React componentshelmet.htmlAttributes.toComponent()helmet.bodyAttributes.toComponent()Defines the configuration schema for the plugin (currently empty).
/**
* Gatsby Node API hook that defines plugin options schema
* @param {Object} params - Parameters object
* @param {Object} params.Joi - Joi validation library from Gatsby
* @returns {Object} Joi schema object for plugin options validation
*/
function pluginOptionsSchema({ Joi });Currently returns an empty schema (Joi.object({})) as the plugin requires no configuration options.
Displays deprecation warning when the plugin initializes.
/**
* Gatsby Node API hook that runs before Gatsby initialization
* @param reporter - Gatsby's reporter API for logging messages
*/
function onPreInit({ reporter }): void;This hook warns users about the plugin's deprecation and recommends migrating to Gatsby's built-in Head API.
The plugin automatically processes and injects the following React Helmet elements:
<title> elements with proper placement logic<meta> tags for SEO, social media, and other metadata<link> elements for stylesheets, canonical URLs, favicons, etc.<script> tags for analytics, tracking, and other JavaScript<style> elements for inline CSS<base> elements for setting document base URL<noscript> elements for fallback content<html> element (lang, class, etc.)<body> element (class, data attributes, etc.)When using React hooks with this plugin, ensure:
import { Helmet } from 'react-helmet'When using with gatsby-plugin-offline, titles may not appear in background tabs. Work around this by adding defer={false} to your Helmet component:
<Helmet title="Page Title" defer={false} />For new projects, consider using Gatsby's built-in Head API instead:
import React from "react";
import { Head } from "gatsby";
function MyPage() {
return (
<>
<Head>
<title>My Page Title</title>
<meta name="description" content="Page description for SEO" />
</Head>
<h1>My Page Content</h1>
</>
);
}
export default MyPage;
export function Head() {
return (
<>
<title>My Page Title</title>
<meta name="description" content="Page description for SEO" />
</>
);
}