question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gatsby-plugin-react-helmet orders the components too late in <head>

See original GitHub issue

Description

When I use gatsby-plugin-react-helmet I notice that the <Helmet /> components come way too late in the head tag. I have noticed that on occasion for instance Facebook does not always parse the og tags if they come too late in the head.

Steps to reproduce

Just use <Helmet />, and notice in the static output files there is a lot of CSS related stuff etc. above the helmet tag in the <head>.

Expected result

Helmet tags should be prioritised when doing the <head /> tag.

Workaround

Currently there is easy workaround, but I consider that gatsby’s default is broken. We should not need workaround for this.

gatsby-ssr.js

var React = require("react");

// Hack, to reorder the helmet components as first in <head> tag
exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
    /**
     * @type {any[]} headComponents
     */
    const headComponents = getHeadComponents();

    headComponents.sort((a, b) => {
        if (a.props && a.props["data-react-helmet"]) {
            return 0;
        }
        return 1;
    });
    replaceHeadComponents(headComponents);
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:26
  • Comments:122 (15 by maintainers)

github_iconTop GitHub Comments

34reactions
ttstausscommented, Jun 2, 2020

I ran into the same issue. I tried checking the meta/og tags via several tools (e.g. Open Graph Check, Meta Tags, and Social Share Preview) and none of them were able to pick them up.

Once I mad a tweak similar to @Ciantic’s I was able to retrieve them successfully.

I beleive this was mainly caused by a rather large style tag being placed before the meta tags.

My solution requried a bit more to get it working. I had to implement the following in my gatsby-ssr.js (you may be able to get away with just using the onRenderbody api, but I used the onPreRenderHTML api as well to move them completely to the top):

const React = require("react")
const { Helmet } = require("react-helmet")

exports.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(),
  ])
}

exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
  const headComponents = getHeadComponents()

  headComponents.sort((x, y) => {
    if (x.props && x.props["data-react-helmet"]) {
      return -1
    } else if (y.props && y.props["data-react-helmet"]) {
      return 1
    }
    return 0
  })

  replaceHeadComponents(headComponents)
}
11reactions
LekoArtscommented, Jun 23, 2020

We are happy to review a PR changing the behavior of gatsby-plugin-react-helmet however we won’t work on this and thus the bot asking for activity is correct. We don’t consider this being broken but a feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gatsby-plugin-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...
Read more >
How to render Helmet Meta Tags before Styles in Gatsby
It seems that the gatsby-plugin-react-helmet renders the meta tags too late in the head section. To test this assumption I briefly checked it ......
Read more >
How to add meta tags with gatsby which first need to be fetched
This means that the Helmet component will be populated later than the crawler requests it. I'm not sure about your specs but you...
Read more >
29 Installing React Helmet #Gatsby #React #PWA ... - YouTube
29 Installing React Helmet #tuto #tutorialReact Helmet is a component which lets you control your document head using their React component.
Read more >
The Ultimate Guide to Gatsby & SEO - SALT.agency®
You can then create an SEO component that uses Gatsby's React Helmet plugin to retrieve the metadata from gatsby-config.js for inclusion in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found