Best of both worlds: Serverless Cloud + Next.js
Tags: nextjs, serverless, react, cloud
Published: 2022-03-10 15:13:55

Originally posted at [Serverless](https://www.serverless.com/blog/best-of-both-worlds-serverless-cloud-next-js)

[Serverless Cloud](https://cloud.serverless.com/?view=register&utm_source=serverless-blog&utm_medium=blog&utm_campaign=serverless-cloud-nextjs) is an amazing development and hosting platform that lets you easily build powerful cloud-based applications. You can build an API in seconds, and it supports file storage, data, scheduled tasks, eventing, and static website hosting.

If you are a React developer, then you may already be familiar with Vercel and Next.js. Vercel is a popular front-end hosting platform that supports Next.js and other front-end frameworks. It has support for server-side rendering, automatic image optimization, and more.

Combining Vercel and Serverless Cloud is a great way to build highly scalable serverless applications that combine the best features of both platforms.

To show you how easy it is to use Serverless Cloud and Vercel together, let’s setup and deploy our [CloudPics example application](https://github.com/serverless/cloud/tree/main/examples/cloud-pics). It’s a React application built with Next.js that lets you upload photos which are stored in Serverless Storage, resized, and indexed in Serverless Data. The home page is visible to both authenticated and non-authenticated users, and it uses server-side rendering to improve SEO and performance for end users.

![Screenshot of the CloudPics app](https://cdn-images-1.medium.com/max/2132/0*DtDUwcxpGfNGzInU.png)

The CloudPics example has two main parts to it: a frontend that uses Next.js and will run on Vercel, and a backend that uses Serverless Cloud.

![](https://cdn-images-1.medium.com/max/2250/0*jQ0RZpHCejdOcS4F.png)

Requests are made from user browsers to the frontend, which routes /api requests to Serverless Cloud. The API implements a simple authentication system, a file upload API that supports image uploads directly to S3, and an API to retrieve the latest uploaded images. As images are uploaded, the Serverless Cloud application reacts to storage events to resize the images, and indexes them in Serverless Data.

We’re going to first get this application running locally, and then deploy it to production with Vercel.

## Setting up

First clone the serverless/cloud repository that contains the example applications:
{% gist https://gist.github.com/richardgrantserverless/e24e9476d1041883e1b3effdb64513d6.js %}
Make sure you have Node.js installed and then install Serverless Cloud and Vercel:
{% gist https://gist.github.com/richardgrantserverless/01cc417dcf93148706046ecfde216038.js %}
## Running the example

To run the example locally we’ll use two terminal windows, one to run the frontend, and one for the backend.

Open up your first terminal window and start the Interactive Cloud Shell. We’ll also run `npm install` the first time, to install our dependencies.
{% gist https://gist.github.com/richardgrantserverless/9f40163fd52acf64ae4a3f79cfe06f90.js %}
This will start up your Cloud developer sandbox and print out it’s endpoint URL:

![](https://cdn-images-1.medium.com/max/3048/0*LawBLyeZvcTQNa7_.png)

Create a file called `.env.local` in the root of the project, and add a variable called `NEXT_PUBLIC_CLOUD_URL` that uses your URL. Make sure you replace the example URL with the one you got when you started cloud.
{% gist https://gist.github.com/richardgrantserverless/0516d3a75ff3f145d982bac559047cd5.js %}
This tells Next.js to send API requests to your Cloud URL. You’ll see this value used in `next.config.js`:
{% gist https://gist.github.com/richardgrantserverless/38e6b9c771e9c468384e5b5ec2d6f788.js %}
You can see that requests to “/api” and “/public” are sent to your Cloud API. The “/public” prefix is where public images are stored by the application.

In a new terminal window you can now start the Next.js development server:
{% gist https://gist.github.com/richardgrantserverless/fb008f35064b4179c67a1d10c1486250.js %}
![](https://cdn-images-1.medium.com/max/3200/0*xl72Kidtj5IplTcn.png)

The Next.js dev server is now running and you can now open “http://localhost:3000" in your browser and upload some images.

Any changes you make to the frontend code are automatically compiled and applied by the Next.js development server. When you change your backend code, your Serverless Cloud developer sandbox is automatically updated.

## Deploying to production

Deploying your Serverless Cloud backend to production couldn’t be simpler, in the terminal where you are running cloud enter:
{% gist https://gist.github.com/richardgrantserverless/2b54f790a1b90d6096e694c9fd40c42e.js %}
This will create a production instance and print out the URL.

![](https://cdn-images-1.medium.com/max/3200/0*4MyW-5njDrwqbb4a.png)

To deploy the frontend to Vercel, quit the Next.js development server and then run:
{% gist https://gist.github.com/richardgrantserverless/9f2f183b02f777769bdbb9d9b3feca7d.js %}
The first time you run this you’ll need to log in to Vercel and it will create a new project in your account. Go to the project settings, and add the `NEXT_PUBLIC_CLOUD_URL` environment variable, and set the value to be your production URL from the previous step.

![](https://cdn-images-1.medium.com/max/2000/0*_BbQvYWmCzkZjnSl.png)

Click Add. Once that’s done, run `vercel — prod` in your terminal again and it will deploy the application and print out the production URL.

Your application is now running in production!

## Go build!

Serverless Cloud and Vercel complement each other perfectly. With Serverless Cloud, you get highly scalable serverless applications that you can develop and test in the cloud and deploy in seconds. With Next.js and Vercel, you can build beautiful frontends with an amazing local developer experience and advanced features like server-side rendering.

Now it’s your turn to build something awesome. Make sure to [check out the docs](https://www.serverless.com/cloud/docs/?utm_source=serverless-blog&utm_medium=blog&utm_campaign=how-serverless-cloud-nextjs) for all the details, [join our Slack channel](https://join.slack.com/t/serverless-contrib/shared_invite/zt-d5qzowja-pnOerTzAIZUrN18hWYUIHA), and follow us for updates. [Reach out and let us know](mailto:cloud@serverless.com) what great things you are building with Serverless Cloud!

Sign up for [Serverless Cloud](https://cloud.serverless.com/?view=register&utm_source=serverless-blog&utm_medium=blog&utm_campaign=serverless-cloud-nextjs)