Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Upgrading to Gatsby v2 With the Help of the Hivemind [Livecoding]

DZone's Guide to

Upgrading to Gatsby v2 With the Help of the Hivemind [Livecoding]

In this livecoding session and quick written tutorial, a web developer shows us how to upgrade to the latest version of Gatsby. Let's get to it Old Sport!

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

You'd think upgrading Gatsby sites to Gatsby 2.0 would be easy. The update guide is only 20 steps long.

Change some package versions. Drop some old packages. Install some new. Tweak some code and you're off to the races.

And then you get stuck.

No error message. No nothing. Just stuck.

Beta software, amirite?

BUT! The internet hivemind comes to the rescue. The beauty of open source software. You do things out in the open, you help when you can, and you ask for help when you can't.

@swyx did an amazing thing and hunted down the source of my woes. It's a GraphQL query compiler bug.

@ryanditjia was nice enough to submit a PR with a workaround. Thanks, Ryan and Shawn!

The New StaticQuery System

Gatsby v2 has a new StaticQuery system, and it's got a bug.

I'm not sure what StaticQuery is supposed to do, or how, but the migration guide says you should use it to get info about your site. Title, description, stuff like that.

I suspect you're meant to change all those export const query = graphql queries into a StaticQuery. Will have to experiment some more, read a doc or two. 

The easiest way to refactor your components to use StaticQuery goes like this:

// src/components/layout.js

export default ({ children }) => (
  <StaticQuery
    query={graphql`
      query LayoutQuery {
        site {
          siteMetadata {
            title
            description
          }
        }
      }
    `}
    render={data => <Layout data={data}>{children}</Layout>}
  />
)

I used it for my Layout component.

StaticQuery takes a query prop, which needs your GraphQL query. The stuff you used to export const query =. And a render prop, which takes the component you want to render once you have data.

When Gatsby fixes the bug, you'll be able to make that code more readable like this:

export default ({ children }) => (
  <StaticQuery
    query={query}
    render={data => <Layout data={data}>{children}</Layout>}
  />
)

Beta software, amirite?

A Tedious Gotcha With Packages

Another gotcha with the upgrade is the manual process to upgrade all your packages.

You have to go into package.json, find everything that starts with gatsby-, and change its version to next. Then you have to hunt through the new Gatsby Plugins Library and see if anything you're using has new peer dependencies.

You have to install peer dependencies yourself. I always forget that one.

For example, Gatsby used to come with React packaged in. Now you have to install it yourself because it's a peer dependency.

There's no telling which of the plugins you're using decided to pull similar tricks moving to Gatsby v2. Alas.

I wish there was a code mod for that, but most people aren't running enough Gatsby sites to make the time savings worth it.

Conclusion

Beta software, amirite?

If you're running Gatsby v1, you should upgrade. Everything seems stable enough. My site, Learn While You Poop is up and running. Ludicrously faster than it was before.

Now I just gotta make the improvements I was planning to make before I got distracted by the upgrade!

Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:
web dev ,react.js ,gatsbyjs ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}