Next.js Deployment

Published on: 14/4/2024

Next.js has always been my go-to framework for building React applications. It is easy to use, has great performance, and has a lot of built-in features that make development easier. I have always used Vercel (who created Next.js) for deployment, and it has been a great experience which I have taken for granted, being able to deploy with one button click and enjoy automated continuous deployment. However, I wanted to deploy a SaaS app which could not be hosted with the Vercel free tier, and I had to explore other deployment options (Vercel was expensive).

However, Next.js is pretty complicated under the hood, with the added server-side functionality that is not present in a traditional client-side React application. This makes deployment a bit more complicated, especially when you want to deploy it on a platform that is not Vercel. With Vercel, you can just push your code to a git repository and it will automatically build and deploy it for you. However, with other platforms, you would have to set up a CI/CD pipeline yourself.

Cloudflare Pages

I first tried deploying a Next.js application with next-on-pages, a CLI tool to help deploy Next.js applications to run on Cloudlfare Pages. I had another Vue app deployed on Cloudflare Pages, and it was a breeze to set up (similar experience to Vercel). However, when trying to deploy the Next.js app, I realised it only supported the edge runtime and not Node.js runtime, which meant that a few of the libraries I used were not supported (next-auth). I don’t think there is any workaround for this, and I had to look for other deployment options (or modify my app to be edge-compatible).

AWS with SST

I came across this interesting project called SST, which uses OpenNext to build Next.js projects into packages that can be deployed across a variety of environments (in this case AWS). It converts (presumably) the server-side functionalities (API routes, Server Actions, Server-side rendering, etc.) into Lambda functions, static assets into S3, and the frontend into CloudFront. It was pretty interesting to see how a simple Next.js app has to be provisioned across so many services to work. However, there were a few quirks that I had to work out.

Docker

In future, I could consider using Docker to containerise my Next.js app and deploy it on a VPS. This would give me more control over the deployment process (I could potentially deploy multiple apps with some docker-compose), and I would also avoid potential costs with AWS in case of any DDOS attacks (at the cost of the website being down).