Tuesday, 26 June, 2018 UTC


Summary

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.
Migrating to @gatsbyjs v2 and getting stuck on this. What am I doing wrong @kylemathews @swyx pic.twitter.com/w48CH0fQrI
— Swizec (@Swizec) June 24, 2018
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 }) => (
   {children}}
  />
)
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 }) => (
   {children}}
  />
)
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
The post Upgrading to Gatsby v2 with the help of the hivemind 👌 appeared first on A geek with a hat.