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.

How to achieve window.onload with Gatsby

See original GitHub issue

I want to add a dynamic meta tags for my website using REST API, I want to achieve something like this: window.onload, If you notice the page is still loading until that function is done, I want that function to be the GET call for my meta tags.

Any ideas?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ascorbiccommented, Jun 4, 2020

How about using react-helmet for the meta tags, and setting the values inside a useEffect hook? This will be run after hydration, so in your app, something like:

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

export const App = ({ children }) => {
  const [metaTag, setMetaTag] = React.useState()

  React.useEffect(() => {
    const loadMetaTag = async () => {
      const tag = await getMetaTag()
      setMetaTag(tag)
    }
    loadMetaTag()
  }, [])

  return (
    <>
      <Helmet>
        {metaTag && <meta name="description" content={metaTag} />}
      </Helmet>
      {children}
    </>
  )
}

1reaction
vladarcommented, Jun 3, 2020

How do you use Gatsby in this situation? Do you have a separate site for each company or do you have a single Gatsby site?

And what metatags are you going to update (and for which purpose)? I am asking because updating some of them via javascript makes little sense (i.e. SEO-related).

It is still not entirely clear what you are trying to achieve, so please share a bit more details.

Also, you can always customize html if you have to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to capture document.ready or window.load events in ...
I think you should add these event listeners in the gatsby-browser.js : // gatsby-browser.js // ES6 export const onClientEntry ...
Read more >
Using Gatsby Script Component to Decrease Page Load ...
In this blog post, I want to walk through some of the decisions you will make when you use the Gatsby Script component....
Read more >
Reactjs – How to capture document.ready or window.load events in ...
I am relatively new to the Gatbsy framework and I am trying to figure out a way to toggle classes on some elements...
Read more >
Improving Gatsby web performance by deferring third-party ...
It was decided that the scripts would load in one of two ways: after a user either scrolled, or navigated to another page....
Read more >
Loading an external JS file using Gatsby
Loading an external JS file using Gatsby. Published in 2020 ... appendChild(script) } export const onClientEntry = () => { window.onload ...
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