This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should subscribe to My Youtube channel to catch me live.

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 }) => (
      query LayoutQuery {
        site {
          siteMetadata {
    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 }) => (
    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 😅

Learned something new? Want to become a better engineer? 💌

Join 9,400+ people just like you already improving their skills.

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖 right away. After that you'll get a thoughtfully written email every week about React, JavaScript,  and lessons learned in my 20 years of writing code for companies ranging from tiny startups to Fortune5 behemoths.

Man, I love your way of writing these newsletters. Often very relatable and funny perspectives about the mundane struggles of a dev. Lightens up my day. ~ Kostas

PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.