When starting a new project I’m often finding myself choosing between Gatsby and Next frameworks. So I decided to write a little post about what those things are for and what to choose in 2021.
I started my journey with Jamstack from Gatsby thanks to my previous team leader which initiated rebuilding a company website using Gatsby as the main framework.
It was an amazing experience. I fell in love with Gatsby as a framework and Jamstack as an approach almost from the very beginning of coding. And when I decided to run a web agency I pushed Jamstack as a primary architecture and Gatsby as a primary framework to use. I didn’t even want to hear that there are alternatives. Gatsby — one love, that’s what I thought at that time.
But the time was passing by and the team started to look around, then we noticed another great React framework Next. Quite similar at the first glance but different at the same time when looking deeper.
Well, both frameworks are created on top of React. What does it mean?
It means that you have all React foundations that give you a create-react-app boilerplate plus additional features, toolkit, and guidelines of how the app should be coded on top.
As I mentioned above, at the first glance they are both fairly similar as they:
create an app from boilerplate allows coding a website significantly faster with awesome developer experience generate SEO-friendly and super-fast websites
But there are two common beliefs:
They rapidly evolve and become kind of monsters.
Let’s start from the very beginning. We generate a new project, open it, and… see a boilerplate structure that both frameworks kindly prepared for us. Same at the first glance but a little bit different.
The folder named public contains built files in the case of Gatsby.js and static assets in the case of Next. The last one creates a .next file for built files.
In both boilerplates, we also have config files where we can provide settings for the project. Both generate different files for us. In Gatsby, a lot of things are going here. All because of plugins.
They build the plugins ecosystem around its functionality and community, which is an important feature you’ll definitely notice as it increases the work speed. Imagine you have a plugin for everything, with a minimum setup time, just paste the code and implement almost every external integration. Cool, isn’t it? I’m sure it’s a killer feature for many devs.
Next doesn’t have this kind of functionality, but both frameworks support all React libraries. In a nutshell, you can implement your integrations in both frameworks, it’s just a case of extended time in Next, but Gatsby does it faster (depending on the complexity of integration, obviously).
And routing. There is a folder “pages” where all our page components will live. Pages will be automatically generated with names of components inside this folder. You should use special link components to route between internal pages.
The rest of the file structure is not really important as you’re free to organize it as you wish: components, context, utils, etc.
Both frameworks were created to make developer’s lives less stressful. Both take care of routine things and allow us to concentrate on the sense of code. They allow us to just create.
The learning of both frameworks is not a hard thing if you already know how to build React-based projects. Both Gatsby and Next have comprehensive documentation and a lot of tutorials. Just jump into it and get things done!
In my opinion, Gatsby.js burst into the room just like Jackie Chan and turned the game around. It has a lot of features, a full list of which you can find on their website. I want to mention my favorite ones.
Gatsby plugins (again)
I’ve already mentioned that Gatsby built an ecosystem of plugins. You can find a plugin for almost everything you need: a lot of gatsby components, gatsby-source plugins, gatsby-integrations, etc. Everything is in their plugin directory for free.
GraphQL for everything
Another amazing thing in Gatsby — GraphQL for all queries. Inside and outside of the project. I had to use it, but now I found it very useful. You can always check your queries in GraphQL Playground that is running together with your project on localhost.
Last in this post but not least — Gatsby.js offers image optimization. They prepared an amazing component called gatsby-image, and this component does everything we need for image optimization. And it has an awesome blurred effect when an image is loading.
Don’t forget about community and support. It’s an important part when choosing a framework, and I would say that both Gatsby.js and Next.js are doing a great job towards community building. In the case of choosing Gatsby.js, you can rely on their Discord community. You can always find an answer and just chat there.
Next.js doesn’t have special plugins and doesn’t use GraphQL out of the box. It has a special image component too, but it doesn’t have a blurred effect 🙂
But Next.js has a lot of other features which make it a favorite of many.
For example, “api” folder is generated from the start. I didn’t mention it in the previous block, but it’s worth it for sure. Every js file in this folder will be executed as a serverless function when a project is hosted on Vercel or Netlify. These functions handle logic like user authentication, form submission, database queries, custom slack commands, and more.
SSG + SSR. What?
Another great thing in Next.js — a hybrid approach. That means that you can combine static site generation (SSG) and server-side rendering (SSR). We got this possibility starting with the 9.3 release and I also found it very useful.
The sophisticated way for updating the content
There are a lot of other great features which you can find in their documentation. I want to mention only one more — incremental static regeneration (ISR). Long story short, if you want to get an idea of that, I suggest jumping into the article written by Lee Robinson for Smashing Magazine, where he explains everything about ISR.
Vercel, Next.js’s parent company, hires ambassadors that make high-quality content for the community. I’ve already mentioned Lee Robinson here, and I suggest taking a look at his YT channel, blog, and Twitter. There is a lot of worthwhile information out there.
When your project is compiled you will get a fast and SEO-friendly website or app. You just have to choose the appropriate hosting platform (Vercel, AWS, Netlify, Cloudflare, etc.) and here we go. Your project is live!
The hosting platforms is a topic that worth making a separate comparison post, which is on my plans. One thing I want to add is that both companies behind those frameworks have their own platforms: Gatsby Cloud and Vercel.
Although Next.js may look like taking leadership, for me, there is no winner in this competition. You can endlessly count the pros and cons of both. In Unicorns, we use both frameworks, and it mainly depends on project needs and structure. Moreover, according to Gatsby’s own comparison, those frameworks are both taking into consideration basic parameters, so it’s mainly a case of personal preferences and skills.
Hope you enjoyed this article and stay tuned!
Previously published at https://agilitycms.com/resources/posts/gatsby-or-next-js-what-to-choose-in-2021