Migrated my blog from Gatsby to Next.js
6 Dec 2022 · 2 min read
My blog were initially created with Gastsby and it has been easy to maintain and writing new blog posts from time to time.
Until recently where I am no longer able to publish new blog post unless I upgrade my project due to dependencies issues.
My choice was to migrate to Next.js because of my experience with other projects that I had set up, all using Next.js.
Below are some of the issues I faced and I decided to make a checklist out for it.
Migration Checklist
-
Add
react,react-domandnextpackages -
Add dev dependencies
autoprefixerandpostcss -
Update scripts
"dev": "next dev -p 8000",
"build": "next build",
"start": "next start",
-
Add
next.config.jsandpostcss.config.js -
Rename directory
/staticto/public -
Move
/templates/*to respective routes. Adapt to dynamic routes. -
Remove
graphqlquery in template files -
Use
getStaticPropsto provide props previously provided by graphql. -
Use
getStaticPathsto generate predefined blog post routes. -
Use
next/headto replacereact-helmetfor header metadata. -
Add
react-markdown,gray-matterandhighlight.jsfor markdown content. -
For deployment, changes needed in
next.config.js.
module.exports = {
webpack: (config, { isServer }) => {
config.module.rules.push(
{
test: /\.md$/,
use: 'raw-loader'
}
)
return config
},
typescript: {
ignoreBuildErrors: true,
},
images: {
unoptimized: true,
},
trailingSlash: true,
}
trailingSlash is important for Next.js to redirect path without ending /. Otherwise, the page will be redirected to /404
unoptimized is set to true because the blog will be exported, and exported build will not enjoy the benefit of optimized image out of the box.
- For deployment to Github pages, add
.nojekyllto the/outdirectory after build. So that Github pages will recognize_nextdirectory and the content inside.
"deploy": "next build && next export && touch out/.nojekyll && gh-pages -t -d out"
The -t flag is important to push the changes on .nojekyll.
That's it!
Cheers.