diff --git a/packages/gatsby-plugin-react-helmet/src/__mocks__/react-helmet.js b/packages/gatsby-plugin-react-helmet/src/__mocks__/react-helmet.js index 9fa11e50cb351..cf6ca1a6b0d90 100644 --- a/packages/gatsby-plugin-react-helmet/src/__mocks__/react-helmet.js +++ b/packages/gatsby-plugin-react-helmet/src/__mocks__/react-helmet.js @@ -1,7 +1,7 @@ const helmet = { htmlAttributes: { toComponent: () => `html-attributes-component` }, bodyAttributes: { toComponent: () => `body-attributes-component` }, - title: { toComponent: () => `title-component` }, + title: { toComponent: () => [{ props: { children: `children` } }] }, link: { toComponent: () => `link-component` }, meta: { toComponent: () => `meta-component` }, noscript: { toComponent: () => `noscript-component` }, diff --git a/packages/gatsby-plugin-react-helmet/src/__tests__/gatsby-ssr.js b/packages/gatsby-plugin-react-helmet/src/__tests__/gatsby-ssr.js index 493f0a57eb712..74dbc13d7b7fc 100644 --- a/packages/gatsby-plugin-react-helmet/src/__tests__/gatsby-ssr.js +++ b/packages/gatsby-plugin-react-helmet/src/__tests__/gatsby-ssr.js @@ -1,3 +1,5 @@ +import { Helmet } from "react-helmet" + jest.mock(`react-helmet`) import { onRenderBody } from "../gatsby-ssr" @@ -16,9 +18,12 @@ describe(`gatsby-plugin-react-helmet`, () => { onRenderBody(actions) + const titleComponent = Helmet.renderStatic().title.toComponent() + expect(actions.setHeadComponents).toHaveBeenCalledTimes(1) + expect(actions.setHeadComponents).toHaveBeenCalledWith([ - `title-component`, + titleComponent, `link-component`, `meta-component`, `noscript-component`, diff --git a/packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js b/packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js index f42476e5abef0..786d7303f891a 100644 --- a/packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js +++ b/packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js @@ -13,13 +13,21 @@ export const onRenderBody = ({ if (setBodyAttributes) { setBodyAttributes(helmet.bodyAttributes.toComponent()) } - setHeadComponents([ - helmet.title.toComponent(), + + const components = [ helmet.link.toComponent(), helmet.meta.toComponent(), helmet.noscript.toComponent(), helmet.script.toComponent(), helmet.style.toComponent(), helmet.base.toComponent(), - ]) + ] + + const titleComponent = helmet.title.toComponent() + + setHeadComponents( + titleComponent?.[0]?.props?.children + ? [titleComponent, ...components] + : components + ) }