- Mehul Mohan
Next.js is an amazing piece of technology which allows you to build truly SEO friendly pages and great performance experience in for users. In this article, I would like to take you through a series of decisions made here at codedamn and why we decided to shift to Next.js, and what Next.js brings to the table.
Journey from React
Before Next.js, codedamn was built using React.js as the core technology. Some of the underlying things included the following:
- Material UI
- Monaco (for our code runner)
- And tons of custom code/packages
Common problems with SPA on production
No matter what anybody says about problems with SPA architecture, the only two problems which are really unfixable are the above two with only SPA in place. With codedamn, this was a big issue for us because we want to reach out to a large audience to help them to learn to code, and for that, we need all the support we can. Leaving out search engines out of the equations was no option.
At codedamn, we tried to pre-render our pages using this service: prerender.io
This SaaS can sit in between your server and client and can smartly detect if a bot is accessing your website (in our case, googlebot or any search engine crawler). If we detect a bot, prerender.io would spin off a headless browser, visit the page, get the rendered HTML and send the response back to the bot. For regular users, we can operate as normal.
But this solution isn't perfect, and to be honest, a little dirty to maintain too. We were looking something permanent and solid, and for a long time we could see Server Side Rendering (SSR) was something inevitable for codedamn, as services like prerender.io fix only the part of the problem. You still have performance issues, and even if you enable pre-rendered pages for everyone, there's a lot of setup and maintenance then, where it is very easy to forget about the core business problem and just be lost in the technical side of things.
Making a shift to Next.js was a bold decision because we had a huge existing infrastructure on React.js alone. But it was worth it. Around July 15th 2020, we decided to move the infrastructure from DigitalOcean based servers running Node.js at backend and React.js at frontend to the following:
- We shifted the whole infrastructure to an almost static Next.js architecture.
- We shifted codedamn infrastructure from DigitalOcean due to cost savings and caching benefits we'll get elsewhere.
- We shifted codedamn's Next.js infrastructure (which included static pages and Next.js lambda functions to Vercel) - the company which created and is maintaining Next.js at the moment
- For GraphQL, we shifted the architecture to AWS Lambdas. This seemed like the best, scalable decision for the platform for now.
- We also shifted our database to MongoDB Atlas.
The good of Next.js
So, in a span of 3 days, we decoupled our architecture into not only many small services, but also across different platforms altogether. This brought its own cons, but way more pros. Sticking to Next.js, here's what we found out.
- The sites were blazingly fast now - thanks to the edge caching Vercel provides and the Automatic Static Optimization built into Next.js
- Codebase migration was not very difficult. There were some file pattern changes, but those were quickly adopted thanks to our code tests in place checking for functionalities.
- Incremental caching - We don't have to invalidate the whole cache when a small change is made to codebase somewhere (thanks to Next.js 9.5)
- Static incremental generation - This is something I love about Next.js. What is better than super fast static webpages? Super fast static webpages which can also update dynamically in the background. This enables us to deploy complete blogs on Next.js (like the one you're reading) with this feature.
- We were finally able to host a dynamic blog on a path (codedamn.com/news which you're reading right now) even though these blog pages you see are statically rendered. We're using Ghost as a headless CMS, but this topic is for some other day.
- We got immediate SEO benefits, correct status codes (like 404 for not found pages, which was not possible earlier with React) without any external SaaS product sitting in between monitoring our each request.
- The complete infrastructure (including lambda functions) is managed by Vercel, which is a big relief as a platform engineer. That means more focus on great code and product, and less focus on the underlying hardware.
- Scaling, caching, and deployment stages are also automated and manageable thanks to Vercel one more time.
The bad of Next.js
There's nothing bad with the framework as such! With the advancements of Next.js, you almost don't need to opt out of their built in mechanism at all. As of the time of the writing (Next.js 9.5), here are some painpoints you might face:
- If you have a huge codebase, it might take some time for you to completely migrate the thing, because Next.js enforces file-based routing pattern which is something unique.
- Next.js brings in first class support for Sass and CSS modules, but you cannot customize it out of the box, you have to opt out of Next.js Sass and CSS processing, and setup your own sass processor, if you want to have support for fine tuned configuration. In our case, we were migrating a codebase which used
babel-plugin-react-css-modulesas a babel plugin to transpile
styleNameas a string to associated
classNameattribute. This is a small change in the configuration but we had to bring in our full custom configuration to implement this. This is not bad really, it's just a place where there's some scope of improvement
There's nothing much on the bad side of Next.js though. If you're a React developer, and you're serious about your projects, shifting to Next.js should be a no-brainer. The company Vercel, maintaining Next.js, is doing a great job at that and the framework will only thrive and improve over the time.