Serverless Containers vs Serverless Next.js SSR on AWS, GCP and Vercel
Tags: cloud, containers, nextjs, react
Published: 2022-09-03 15:25:04

#ULTIMATE SUMMARY 2022
#Introduction
We have so many tools to get the job done in cloud that it's easy to get lost, [The Paradox in Choice](https://en.wikipedia.org/wiki/The_Paradox_of_Choice)
I'm writing this article to help you feel better by reducing the optionality. The main question is : do you want to have your application portable or you are satisfied with the dependency on a specific cloud provider. Naturally portability pays with complexity, have you ever heard about the [multi stage build](https://docs.docker.com/develop/develop-images/multistage-build/) ? Let's get to it.

#Common setup
In this guild I'm using `yarn`, `next 12.2.0`, `react 18.2.0`.

You may wish to get more information about the [output standalone](https://nextjs.org/docs/advanced-features/output-file-tracing)

The need to run an app in the container or with the node runtime is due to the [next export limitations](https://nextjs.org/docs/advanced-features/static-html-export)

I've got two domains : nextaws.tk and nextgcp.tk for the tests on [freenom](https://www.freenom.com/).

#Deployments : 
- [AWS Amplify Fargate](https://dev.to/hexfloor/serverless-containers-vs-serverless-nextjs-ssr-on-aws-amplify-fargate-2pkh)
- [AWS Amplify Lambda](https://dev.to/hexfloor/serverless-containers-vs-serverless-nextjs-ssr-on-aws-amplify-lambda-4bbc)
- [GCP App Engine](https://dev.to/hexfloor/serverless-containers-vs-serverless-nextjs-ssr-gcp-app-engine-p3d)
- [GCP Cloud Run](https://dev.to/hexfloor/serverless-containers-vs-containerless-nextjs-ssr-gcp-cloud-run-4dgk)
- [Vercel](https://vercel.com/solutions/nextjs)

#Limitations
First of all, check the [Next.js deployment guide](https://nextjs.org/docs/deployment).
Known limitations :
- [Netx.js serverless](https://nextjs.org/blog/next-8#serverless-nextjs) announced in 2019 [has been deprecated](https://nextjs.org/docs/messages/deprecated-target-config) in [2021](https://github.com/vercel/next.js/issues/20790).
- some features are not supported in the `serverless` mode, see [SSR Amplify supported and unsupported Next.js features](https://docs.aws.amazon.com/amplify/latest/userguide/server-side-rendering-amplify.html)
- [internationalization](https://github.com/i18next/next-i18next) doesn't seem to work out of the box in the `serverless` mode, you may be interested by [issue_274](https://github.com/i18next/next-i18next/issues/274), [issue_911](https://github.com/i18next/next-i18next/issues/911), [issue_1311](https://github.com/i18next/next-i18next/issues/1311), [issue_1490](https://github.com/i18next/next-i18next/issues/1490), [issue_1552](https://github.com/i18next/next-i18next/issues/1552)
- when using some other modern libraries like [tailwind](https://tailwindcss.com/), you have to update the `Next.js` version carefully or you may have [some errors](https://nextjs.org/docs/messages/react-hydration-error) following the `Next.js` update. Nothing new in the javascript world, if something is working it's not guaranteed [it will last](https://github.com/left-pad/left-pad/issues/4) :) It is important to really read the [Next.js release notes](https://github.com/vercel/next.js/releases) as the base configuration may change in between the releases.
- hosting of the `Netx.js` apps on Vercel naturally guarantees better feature and update support than hosting on any other cloud provider, consequently the technology itself is not cloud agnostic - it is what it is, as of today hosting in containers is more or less equivalent to the hosting on Vercel - it may change tomorrow, check on the [serverless framework](https://www.serverless.com/plugins/serverless-nextjs-plugin)
- as of today `Next.js` have better feature support in containers than in `serverless` mode and thus you have to be aware of the limitations
- `Next.js` today offers great simplicity and performance, however at some point you are almost forced to switch to the SSR and thus the question should be formulated in the following way : are you ready to have the `node.js` serving `js` files on the server or you wish to have the `js` files executed in the browser only? You may often migrate to [react](https://reactjs.org/) by applying the [From React to Next.js](https://nextjs.org/learn/foundations/from-react-to-nextjs) in reverse mode.

#Summary
With all of the above I hereby may conclude that : 

- the best way to deploy a `Next.js` application is Vercel
- the best way to deploy a `Next.js` application on GCP is Cloud Run 
- the best way to deploy a `Next.js` application on AWS is Amplify Lambda, however you will have to add custom i18n
- at some point you will have to switch to SSR in your`Next.js` application - this is the price to pay for the performance and the high-end look

I'm happy to use `Next.js` and at the same time I would appreciate to have more direction predictability. Quo vadis, `Next.js` ? Few years ago the move was towards serverless, today it's containers, will it be all-the-time-up-node-js instance tomorrow ? :) Next.js or There and Back Again.