Update Gatsby 2 to 4
After watching the GatsbyConf 2022 presentation Upgrading from Gatsby 2 to Gatsby 4—It's About Time, I don't feel much more ready but I'm ready to try again. One thing I noticed is Khaled never did clear out node_modules
.
Initial run of npm outdated
Everything is Green under Wanted but going for Latest!
Package | Current | Wanted | Latest | Location |
---|---|---|---|---|
@mdx-js/mdx | 1.6.22 | 1.6.22 | 2.1.0 | node_modules/@mdx-js/mdx |
@mdx-js/react | 1.6.22 | 1.6.22 | 2.1.0 | node_modules/@mdx-js/react |
babel-plugin-styled-components | 1.13.3 | 1.13.3 | 2.0.6 | node_modules/babel-plugin-styled-components |
gatsby | 2.32.13 | 2.32.13 | 4.10.0 | node_modules/gatsby |
gatsby-image | 2.11.0 | 2.11.0 | 3.11.0 | node_modules/gatsby-image |
gatsby-plugin-manifest | 2.12.1 | 2.12.1 | 4.10.0 | node_modules/gatsby-plugin-manifest |
gatsby-plugin-mdx | 1.10.1 | 1.10.1 | 3.10.0 | node_modules/gatsby-plugin-mdx |
gatsby-plugin-offline | 3.10.2 | 3.10.2 | 5.10.0 | node_modules/gatsby-plugin-offline |
gatsby-plugin-postcss | 3.7.0 | 3.7.0 | 5.10.0 | node_modules/gatsby-plugin-postcss |
gatsby-plugin-react-helmet | 3.10.0 | 3.10.0 | 5.10.0 | node_modules/gatsby-plugin-react-helmet |
gatsby-plugin-sharp | 2.14.4 | 2.14.4 | 4.10.0 | node_modules/gatsby-plugin-sharp |
gatsby-plugin-styled-components | 3.10.0 | 3.10.0 | 5.10.0 | node_modules/gatsby-plugin-styled-components |
gatsby-remark-images | 3.11.1 | 3.11.1 | 6.10.0 | node_modules/gatsby-remark-images |
gatsby-source-filesystem | 2.11.1 | 2.11.1 | 4.10.0 | node_modules/gatsby-source-filesystem |
gatsby-transformer-sharp | 2.12.1 | 2.12.1 | 4.10.0 | node_modules/gatsby-transformer-sharp |
prettier | 2.1.2 | 2.1.2 | 2.6.0 | node_modules/prettier |
react | 16.14.0 | 16.14.0 | 17.0.2 | node_modules/react |
react-dom | 16.14.0 | 16.14.0 | 17.0.2 | node_modules/react-dom |
tailwindcss | 2.2.19 | 2.2.19 | 3.0.23 | node_modules/tailwindcss |
Update all the things
npm update --save
is not working for me like yarn upgrade
did in the video. I expected everything to magically change to the newest versions. It's probably better in the long run. I headed back to the official Gatsby site migration articles.
1git checkout -b update-to-gatsby-42npm install gatsby@latest --legacy-peer-deps # to 4.10.03npm install gatsby-image@latest # not a thing to do anymore!
What changed?
gatsby
is now 4.10.0 but that is it, with or without the--legacy-peer-deps
flaggatsby-image
is deprecated and I should be using gatsby-plugin-image
Migrate gatsby-image to gatsby-plugin-image
Again, off to the official documentation for this. This is the usual install, configure Gatsby plugin pattern with an added running of the codemod. That is new to me!
1npm install gatsby-plugin-image gatsby-plugin-sharp@latest gatsby-transformer-sharp@latest --legacy-peer-deps
Configure Plugins
1module.exports = {2 plugins: [3 `gatsby-plugin-image`,4 `gatsby-plugin-sharp`,5 `gatsby-transformer-sharp`,6 ],7}
Run the codemod
1npx gatsby-codemods gatsby-plugin-image <optional-path>
Message received (codemod)
It appears you're passing a variable to your image component. We haven't changed it, but we have updated it to use the new GatsbyImage component. Please check src/components/CoverImage.js manually.
Lots of little things and I'm not sure if any of them worked. Mainly replacing fluid { ...GatsbyImageSharpFluid}
with gatsbyImageData(layout: FULL_WIDTH)
in graphql and then in how it is being used by GatsbyImage
component, which automatically replaced Img
component from old gatsby-image
.
In Image, I had originalName in the fluid {} and I just tried moving it out since I needed it. That's not going to work I bet!
I was able to use StaticImage
instead.
Manual Changes (codemod)
Not sure if I have any of this, if so, check the site
- Images using static query
- If using
src
for an SEO component, usegetSrc
Finishing up migration
Uninstall gatsby-image as it is no longer used. It is mentioned in a post but that's okay.
1npm uninstall gatsby-image
All the changes, if any, to npm packages. I ran them all manually one at a time.
1npm install gatsby-plugin-google-fonts@latest # no update2npm install gatsby-plugin-manifest@latest # to 4.10.03npm install gatsby-plugin-mdx@latest # to 3.10.04npm install gatsby-plugin-offline@latest # to 5.10.05npm install gatsby-plugin-postcss@latest # to 5.10.06npm install gatsby-plugin-react-helmet@latest # to 5.10.07npm install gatsby-plugin-styled-components@latest # to 5.10.08npm install gatsby-remark-images@latest # to 6.10.09npm install gatsby-source-filesystem@latest # to 4.10.010npm install mdx-utils@latest # no update11npm install @mdx-js/mdx@latest --legacy-peer-deps # to 2.1.0 (gatsby-plugin-mdx requires v1)12npm install @mdx-js/react@latest --legacy-peer-deps # to 2.1.0 (gatsby-plugin-mdx requires v1)13npm install @tailwindcss@latest # to 3.0.2314npm install autoprefixer@latest # to 10.4.415npm install styled-components@latest # to 5.3.316npm install babel-plugin-styled-components@latest # to 2.0.617npm install postcss@latest # to 8.4.1218npm install react@latest # to 17.0.219npm install react-dom@latest # to 17.0.220npm install prism-react-renderer@latest # to 1.3.121npm install prop-types@latest # to 15.8.122npm install react-headroom@latest # to 3.2.023npm install react-helmet@latest # no update24npm install react-live@latest # to 2.4.125npm install twin.macro@latest # to 2.8.226npm install prettier@latest # to 2.6.0
I had to rollback @mdx-js/mdx and @mdx-js/react because gatsby-plugin-mdx requires v1 of those.
Somehow, everything is working?
Yes, yes, I'm shocked! Besides all the npm
installs, here's what I did.
Removed
gatsby-plugin-image
from gatsby-config.jsChanged
purge
tocontent
in tailwind.config.jsRemoved Image component as it was only used in Hero component / Removed from components/index.js
Created a Moog component that displays the main cover image inside of the Hero component
1import { StaticImage } from 'gatsby-plugin-image'2
3const Moog = () => (4 <StaticImage5 src='../images/steve-harvey-xWiXi6wRLGo-unsplash.jpg'6 alt='MOOG Modular Synthesizer - photo by Steve Harvey @ Unsplash'7 className='object-cover h-full min-w-full'8 />9)
- Removed absolute in Img when converting to GatsbyImage in PostCard component
Final gatsby-image updates
In: CoverImage.js, PostCard.js, single-post.js
- Convert from Img in
gatsby-image
to GatsbyImage ingatsby-plugin-image
1-fluid {2- ...GatsbyImageSharpFluid3-}4+gatsbyImageData(layout: FULL_WIDTH)56-<Img fluid=7+<GatsbyImage image=89// In GraphQL10childImageSharp {11- fluid {12- ...GatsbyImageSharpFluid13- }14+ gatsbyImageData(layout: FULL_WIDTH)15}
Final npm outdated run
These are fine, as mentioned previously, v1 is required by gatsby-plugin-mdx
Package | Current | Wanted | Latest | Location |
---|---|---|---|---|
@mdx-js/mdx | 1.6.22 | 1.6.22 | 2.1.0 | node_modules/@mdx-js/mdx |
@mdx-js/react | 1.6.22 | 1.6.22 | 2.1.0 | node_modules/@mdx-js/react |
Everything worked locally, but...
When the Github Action ran, it failed! The error related to styled-components
.
Can't resolve 'react-is' in '/home/runner/work/koamar.com/koamar.com/node_modules/styled-components/dist'
According to the styled-components FAQ, styled-components now requires react-is@^16.8 (or higher hopefully). I updated that and started building again. That time, it was successful.
Local vs. Github
This all worked locally. I of course tested in gatsby develop
but also with gatsby build
. I never saw an error related to styled-components
!
Attributions
Image by unDraw