Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    Upgrading to Gatsby v2 with the help of the hivemind 👌

    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 }) => (
      <staticquery query="{graphql`" layoutquery="" {="" site="" sitemetadata="" title="" description="" }="" `}="" render="{data" ==""> <layout data={data}>{children}</layout>}
      />
    )
    </staticquery>
    

    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>}
      />
    )
    </staticquery>
    

    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 😅

    Published on June 26th, 2018 in Livecoding, Side Projects, Technical

    Did you enjoy this article?

    Continue reading about Upgrading to Gatsby v2 with the help of the hivemind 👌

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️