DEV Community

Kevin Meyvaert
Kevin Meyvaert

Posted on

Fixing hanging Gatsby 2 builds on Netlify

I have been working on a music festival website as a pet project for several years now. Last year I decided to rebuild the entire thing using the JAM Stack. Contentful + Gatsby + Netlify. It's great, fast, scalable and for my usage, completely free.

A few months ago Gatsby released their v2 and I was eager to update the website and use all the new fancy stuff. However, shortly after upgrading, when making a build via Netlify, the builds got stuck at "Cached node version vx.x.x" step, and eventually timed out.

This was no fun

I did about 40 commits making slight changes to the code, reverting dependencies, trying different Node versions, but nothing worked. I decided to stop trying after I lost entire weekends trying to solve the issue.

I'm one of those stubborn developers. I had to fix this myself.🤦🏻‍♂️ Meanwhile, some months had passed, and I really had to start working on the 2019 edition website. For once, I had to call in the support cavalry.

Finally I got help from Chris & Dennis from Netlify support (shout out, you made my day). They pointed me to an issue on Github logged some months ago during Gatsby v2 Beta - where they address a bug, which is still in the latest version.

As it appears. The failing build is caused by a Jest worker child spawned by Gatsby during the build. But when the parent Jest worker is exited, the child keeps existing, causing the build on Netlify to time out.

Solution

TL;DR
Don't use setTimeout on componentWillMount. But when you do, make sure you only execute it during runtime. (wrap it in a typeof window !== 'undefined' check)

I know I know

Also. Don't use componentWillMount at all. Use hooks, like all cool kids do.😎🆒

Hooks are so cool omgwtfbbq

Anyway, this issue made me lose a lot of time and gave me my first grey hairs. I hope writing this will help someone in the near future.

PS: This issue also occurs when using certain firebase or aws-amplify functions in componentWillMount - as they also use setTimeouts.

Top comments (0)