Cloudflare Docs
Pages
Visit Pages on GitHub
Set theme to dark (⇧+D)

Deploy a Next.js site

Next.js is an open-source React framework for creating websites and apps. In this guide, you will create a new Next.js application and deploy it using Cloudflare Pages.

​​ Consider if you need the Edge Runtime

Cloudflare Pages supports full-stack Next.js projects which use the Edge Runtime or any projects which can be statically exported. The Edge Runtime allows applications to use server-side features such as Edge API Routes, server-side rendering (SSR) pages with getServerSideProps() and Middleware.

For more information about the Edge Runtime, refer to the official Next.js documentation which explains the differences between the Edge Runtime and traditional Node.js servers, or read the Cloudflare announcement blog post.

​​ Before you continue

All of the framework guides assume you already have a fundamental understanding of Git. If you are new to Git, refer to this summarized Git handbook on how to set up Git on your local machine.

If you clone with SSH, you must generate SSH keys on each computer you use to push or pull from GitHub.

Refer to the GitHub documentation and Git documentation for more information.

​​ Create a new project using the Edge Runtime

Create a new project using npx or yarn by running either of the following commands in your terminal:

$ npx create-next-app my-app
# or
$ yarn create next-app my-app

After creating your project, a new my-app directory will be generated using the official default template.

​​ Configure the project to use the Edge Runtime

The default template includes a traditional Node.js-powered API route (pages/api/hello.js). Delete this file. It is incompatible with the Edge Runtime. To use API routes, refer to Next.js’ Edge API Routes documentation, which offers a standards-based equivalent.

As an example, the existing pages/api/hello.js file re-written as an Edge API Route would look like this:

pages/api/hello.js
// Next.js Edge API Routes: https://nextjs.org/docs/api-routes/edge-api-routes
export const config = {
runtime: 'edge',
}
export default async function handler(req) {
return new Response(JSON.stringify({ name: 'John Doe' }))
}
pages/api/hello.ts
// Next.js Edge API Routes: https://nextjs.org/docs/api-routes/edge-api-routes
import type { NextRequest } from 'next/server'
export const config = {
runtime: 'edge',
}
export default async function handler(req: NextRequest) {
return new Response(JSON.stringify({ name: 'John Doe' }))
}

Next, you must configure the rest of the project to use the Edge Runtime.

You can opt in on individual pages by exporting the following from each page:

export const config = {
runtime: "edge",
};

Or configure the whole application to use the Edge Runtime by setting the runtime globally:

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
experimental: {
runtime: 'edge',
}
}
module.exports = nextConfig

Refer to Next.js’ documentation about the Edge Runtime for more information.

​​ Create a GitHub repository

Create a new GitHub repository by visiting repo.new. After creating a new repository, prepare and push your local application to GitHub by running the following commands in your terminal:

$ git remote add origin https://github.com/<your-gh-username>/<repository-name>
$ git branch -M main
$ git push -u origin main

​​ Deploy with Cloudflare Pages

Deploy your site to Pages:

  1. Log in to the Cloudflare dashboard.

  2. In Account Home, select Pages > Create a project.

  3. Select the new GitHub repository that you created and, in the Set up builds and deployments section, select Next.js as your Framework preset. Your selection will provide the following information.

    Configuration optionValue
    Production branchmain
    Build commandnpx @cloudflare/next-on-pages --experimental-minify
    Build directory.vercel/output/static
  4. Next.js requires Node.js v16 or later to build successfully. To set your Node version, go to Settings in your Pages project > Environment Variables (advanced) section and add a NODE_VERSION variable with a value of 16 or greater.

After configuring your site, you can begin your first deploy. You should see Cloudflare Pages installing @cloudflare/next-on-pages, your project dependencies, and building your site before deploying it.

​​ Create a new static project

Create a new project using npx or yarn by running either of the following commands in your terminal:

$ npx create-next-app --example with-static-export my-app
# or
$ yarn create next-app --example with-static-export my-app

After creating your project, a new my-app directory will be generated using the official with-static-export example as a template.

​​ Create a GitHub repository

Create a new GitHub repository by visiting repo.new. After creating a new repository, prepare and push your local application to GitHub by running the following commands in your terminal:

$ git remote add origin https://github.com/<your-gh-username>/<repository-name>
$ git branch -M main
$ git push -u origin main

​​ Deploy with Cloudflare Pages

Deploy your site to Pages:

  1. Log in to the Cloudflare dashboard.

  2. In Account Home, select Pages > Create a project.

  3. Select the new GitHub repository that you created and, in the Set up builds and deployments section, select Next.js (Static HTML Export) as your Framework preset. Your selection will provide the following information.

    Configuration optionValue
    Production branchmain
    Build commandnpm run export
    Build directoryout
  4. Next.js requires Node.js v12.22.0 or later to build successfully. To set your Node version, go to Settings in your Workers project > Environment Variables (advanced) section and add a NODE_VERSION variable with a value of 12.22.0 or greater.

After configuring your site, you can begin your first deploy. You should see Cloudflare Pages installing next, your project dependencies, and building your site before deploying it.

​​ Preview your site

After deploying your site, you will receive a unique subdomain for your project on *.pages.dev. Every time you commit new code to your Next.js site, Cloudflare Pages will automatically rebuild your project and deploy it. You will also get access to preview deployments on new pull requests, so you can preview how changes look to your site before deploying them to production.

For the complete guide to deploying your first site to Cloudflare Pages, refer to the Get started guide.

​​ Use bindings in your Next.js application

A binding allows your application to interact with Cloudflare developer products, such as KV, Durable Object, R2, and D1.

In Next.js, add server-side code via API Routes and getServerSideProps. Then access bindings set for your application by accessing them in your code via process.env.

The following code block shows an example of accessing a KV namespace in Next.js.

src/index.tsx
// ...
export async function getServerSideProps({ req }: GetServerSidePropsContext) => {
// the type `KVNamespace` comes from the @cloudflare/workers-types package
const { MY_KV } = (process.env as { MY_KV: KVNamespace }));
return {
// ...
};
};

​​ Learn more

By completing this guide, you have successfully deployed your Next.js site to Cloudflare Pages. To get started with other frameworks, refer to the list of Framework guides.