DEV Community

Cover image for How to Deploy Your Next.js App to Cloudflare
Arindam Majumder
Arindam Majumder Subscriber

Posted on

How to Deploy Your Next.js App to Cloudflare

Introduction

Most of the Developers, including me, deploy their Next.js application to Vercel—it’s quick, easy, and seamless.

But one day, I started thinking “Could there be a better or simpler way to deploy my app?”

Bug Books: Joey Paul Online: Pantsing A Series - The Creative Process

After exploring a bit I came across Cloudflare Pages, and I was impressed by how straightforward it is to deploy your Next.js app there.

Cloudflare Pages is a great alternative to Vercel and Netlify because it offers unlimited bandwidth for free, while others put limits on how much we can use. Plus, there’s no catch on the free plan—it allows commercial use, unlike Vercel’s restrictions.

In this article, we’ll explore how to deploy your Next.js app to Cloudflare Pages.


Prerequisites

Before we start, make sure that you have the following:

  1. A Cloudflare account. You can sign up at Cloudflare.

  2. A GitHub, GitLab, or Bitbucket account with a repository containing our Next.js code.

  3. Node.js installed on your local machine.


Basic Project Setup

Firstly, Let’s start by creating a Simple Next.js Application. For the simplicity of this article, I’ve already created a demo project.

You can clone the code from GitHub using the following command:

git clone -b starter https://github.com/Arindam200/next-on-cloudflare.git
Enter fullscreen mode Exit fullscreen mode

After cloning the repository, let’s go to the project directory:

cd next-on-cloudflare
Enter fullscreen mode Exit fullscreen mode

Next, we’ll install the dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

To work with Cloudflare, the first thing we need to make sure is that all the pages are configured to run on the Edge runtime. For that we’ll to add the following line on every page.

export const runtime = "edge";
Enter fullscreen mode Exit fullscreen mode

This configuration will make sure that our pages run on the edge runtime.

Now, we’ll need a package to build our application. Let’s install it by running the following command:

npm install --save-dev @cloudflare/next-on-pages
Enter fullscreen mode Exit fullscreen mode

Once the downloading process is done, we’ll update the package.json to include the following scripts

"pages:build": "npx @cloudflare/next-on-pages",
"preview": "npm run pages:build && wrangler pages dev",
"deploy": "npm run pages:build && wrangler pages deploy"
Enter fullscreen mode Exit fullscreen mode

Here’s how your package.json will look after the update.

Screenshot of a package.json file in a code editor with details about scripts, dependencies, and devDependencies for a project named

Next, we’ll create a wrangler.toml file in the root directory of our application with the following content:

//wrangler.toml
name = "next-on-cloudflare"
compatibility_date = "2024-07-29"
compatibility_flags = ["nodejs_compat"]
pages_build_output_dir = ".vercel/output/static"
Enter fullscreen mode Exit fullscreen mode

Finally, we’ll update the next.config.mjs with the following

//next.config.mjs
import { setupDevPlatform } from '@cloudflare/next-on-pages/next-dev';

/** @type {import('next').NextConfig} */
const nextConfig = {};

 if (process.env.NODE_ENV === 'development') {
   await setupDevPlatform();
 }

export default nextConfig;
Enter fullscreen mode Exit fullscreen mode

And with that, our basic project setup is done!

If we run the project and go to localhost:3000 and we’ll get the following UI

Screenshot of a web page titled


Deployment

Now that our project is ready, it’s time to deploy it to Cloudflare Pages.

  1. Log in to the Cloudflare dashboard and select your account.

  2. In Account Home, select Workers & Pages.

  3. Select Create application > Pages > Connect to Git.

Here we have to connect to Git to import a repository. Alternatively, You can directly upload the codebase as well.

Dashboard interface for Cloudflare Workers & Pages, featuring options to start by importing a Git repository or direct upload. The sidebar displays various tools and features like Durable Objects, R2, and more.

After connecting to Github, Select the repo that you want to deploy. For this tutorial, we’ll deploy the repo that we cloned i.e. next-on-cloudflare.

Cloudflare deployment interface showing options to deploy a site from a GitHub account. The user

Once the repository is connected, We’ll configure our build settings. Cloudflare Pages will automatically detect the settings for most Next.js projects, but make sure to set the following for the build:

  • Framework: Select Next.js.

  • Build Command: Use npx @cloudflare/next-on-pages@1 or whatever you set up in your package.json.

  • Output Directory: Set this to .vercel/output/static (or the appropriate directory for your project).

Screenshot of a web interface for setting up builds and deployments. The project name is

Finally, Let’s deploy our site. click Deploy to start the deployment process. Cloudflare will automatically build and deploy our app.

Screenshot build setup

Now, let’s go to https://next-on-cloudflare-arindam.pages.dev/ and we can see our Next.js app up and running on Cloudflare Pages.

Screenshot of Deployed site

It’s as simple as that!


Conclusion

In this article, we have created and deployed our Next.js application to Cloudflare. With this foundation, you can now build and deploy even more complex applications with ease.

If you found this blog post helpful, please consider sharing it with others who might benefit. Also, Follow me For More Content like this:

For Paid collaboration mail me at : arindammajumder2020@gmail.com.

Thank you for Reading till the end.

GIF thank you grateful gratitude - animated GIF on GIFER

Happy Coding!

Top comments (1)

Collapse
 
srbhr profile image
Saurabh Rai

Is there any difference in vercel vs Cloudflare?
IMHO vercel offers analytics, speed insights, firewall etc. Which is good