Swizec Teller - a geek with a hatswizec.com

    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>}

    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 ๐Ÿ˜…

    Did you enjoy this article?

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

    Learned something new?
    Want to become an expert?

    Here's how it works ๐Ÿ‘‡

    Leave your email and I'll send you thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

    Join Swizec's Newsletter

    And get thoughtful letters ๐Ÿ’Œ on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.

    "Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. ๐Ÿ‘Œ"

    ~ Ashish Kumar

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. โœŒ๏ธ

    Have a burning question that you think I can answer?ย I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

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

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first ๐Ÿ’ฐ on the side with ServerlessReact.Dev

    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 โค๏ธ