skip to Main Content

My Gatsby website is not generating proper title tags on SSR. When I build the website all I get on my generated files are <title data-react-helmet="true"></title>. I’d appreciate help to find the problem and solve as I have no idea which might be the issue after a long debugging process.

Relevant files:

package.json

...
"dependencies": {
  "gatsby": "^2.19.45",
  "gatsby-image": "^2.2.44",
  "gatsby-plugin-manifest": "^2.2.48",
  "gatsby-plugin-react-helmet": "^3.2.2",
  "gatsby-plugin-sharp": "^2.4.13",
  "gatsby-plugin-sitemap": "^2.3.1",
  "gatsby-plugin-typescript": "^2.3.3",
  "gatsby-source-filesystem": "^2.1.56",
  "gatsby-source-graphql": "^2.2.0",
  "react": "^16.12.0",
  "react-dom": "^16.12.0",
  "react-helmet": "^6.0.0",
}
...

gatsby.config.js

  plugins: [
    `gatsby-plugin-react-helmet`,
    ...
  ]

(gatsby-plugin-offline is disabled)

Seo.tsx

import React from "react"
import { Helmet } from "react-helmet"
import { useStaticQuery, graphql } from "gatsby"

interface Props {
  title: string
  description?: string
  image?: string
}

const SEO = ({ title, description, image }: Props) => {
  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadata {
            title
            description
            image
            siteUrl
          }
        }
      }
    `
  )

  const metaDescription = description || site.siteMetadata.description
  const shareImage = image || site.siteMetadata.image
  const url = site.siteMetadata.siteUrl

  return (
    <Helmet defer={false}>
      <title>{title}</title>
      <meta name="description" content={metaDescription} />
      <meta name="image" content={shareImage} />
      <link rel="canonical" href={url} />
      <meta property="og:url" content={url} />
      <meta property="og:type" content="website" />
      <meta property="og:title" content={title} />
      <meta property="og:description" content={metaDescription} />
      <meta property="og:image" content={shareImage} />
      <meta name="twitter:card" content="summary_large_image" />
      <meta name="twitter:title" content={title} />
      <meta name="twitter:description" content={metaDescription} />
      <meta name="twitter:image" content={shareImage} />
    </Helmet>
  )
}

export default SEO

Changing the <title>{title}</title> to <Helmet title={title}> or either removing defer={true} won’t change anything in the result.

gatsby-ssr.js

import React from "react"
import { Helmet } from "react-helmet"

export const onRenderBody = (
  { setHeadComponents, setHtmlAttributes, setBodyAttributes },
  pluginOptions
) => {
  const helmet = Helmet.renderStatic()
  setHtmlAttributes(helmet.htmlAttributes.toComponent())
  setBodyAttributes(helmet.bodyAttributes.toComponent())
  setHeadComponents([
    helmet.title.toComponent(),
    helmet.link.toComponent(),
    helmet.meta.toComponent(),
    helmet.noscript.toComponent(),
    helmet.script.toComponent(),
    helmet.style.toComponent()
  ])
}

I still have problems with an empty srr file.

On any given page I call the SEO tag, for example:

<SEO title="Hello World" description="Foo Bar" />

2

Answers


  1. Chosen as BEST ANSWER

    Found the solution. It's a problem with redux-persist-store that was causing the SSR to not work.

    The full solution can be found in another question here: Gatsby not generating correct static HTML files


  2. Can you try something like title = {title} passing it in as a props of helmet rather than as the title tag you have like above

      <Helmet
          htmlAttributes={{
            lang,
          }}
          title={title}
          titleTemplate={`%s | ${site.siteMetadata.title}`}
          meta={[
            {
              name: `description`,
              content: metaDescription,
            },
            {
              property: `og:title`,
              content: title,
            },
            {
              property: `og:description`,
              content: metaDescription,
            },
            {
              property: `og:type`,
              content: `website`,
            },
            {
              name: `twitter:card`,
              content: `summary`,
            },
            {
              name: `twitter:creator`,
              content: site.siteMetadata.author,
            },
            {
              name: `twitter:title`,
              content: title,
            },
            {
              name: `twitter:description`,
              content: metaDescription,
            },
          ].concat(meta)}
        />
    

    The Gatsby Starter Default Template has a good SEO component you can reference as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search