Next.jsCloudflareDeploymentvinextCI/CD

The complete guide to deploying Next.js on Cloudflare Workers

Everything you need to go from a local Next.js App Router project to a globally distributed Cloudflare Workers deployment — with vinext, Vite, custom domains, and CI/CD.

Ajay · Oct 20, 2026 · 18 min read
The complete guide to deploying Next.js on Cloudflare Workers
Video Tutorial
Step-by-step walkthrough: Deploying Next.js to Cloudflare Workers with vinext

Most Next.js tutorials end at npm run build. But shipping a production app means choosing an edge platform, wiring up SSR, configuring custom domains, and automating deploys. This guide walks through the entire journey of deploying a Next.js App Router application to Cloudflare Workers using vinext — from first install to a fully automated CI/CD pipeline.

Why Cloudflare Workers?

Traditional Node.js hosting runs your server in a single region. Cloudflare Workers run your code across 300+ edge locations worldwide, which means:

  • Sub-50ms cold starts — Workers use V8 isolates instead of full Node.js processes.
  • Global latency reduction — your HTML is rendered at the PoP closest to the user.
  • Automatic scaling — no capacity planning, no load balancers to configure.
  • Built-in DDoS protection — Cloudflare's network sits in front of your app.

For a content-heavy site like a blog, the combination of server-side rendering at the edge and aggressive caching makes Cloudflare Workers an excellent fit.

Next.js to Cloudflare Workers Architecture Overview: vinext compiler, Vite bundler, and Cloudflare Edge Network
Next.js to Cloudflare Workers Architecture Overview: vinext compiler, Vite bundler, and Cloudflare Edge Network

Prerequisites

Before you start, make sure you have:

  • Node.js 18+ installed
  • A Cloudflare account (free tier works)
  • A Next.js App Router project (Next.js 14+ recommended)
  • Wrangler CLI — Cloudflare's deployment tool

Log in to your Cloudflare account via Wrangler:

Terminal
npx wrangler login

This opens a browser window for OAuth. Once authenticated, Wrangler stores your credentials locally.

Step 1 — Install vinext and dependencies

vinext is Cloudflare's adapter that bridges Next.js App Router with Vite and Cloudflare Workers. Install it alongside the required Vite plugins:

Terminal
npm install vinext @cloudflare/vite-plugin @vitejs/plugin-rsc vite --save-dev

What vinext does under the hood. It takes your Next.js App Router code — Server Components, route handlers, middleware — and compiles it into a Vite-based build that outputs a Cloudflare Worker bundle. The Worker handles SSR at the edge while static assets are served from Cloudflare's CDN.

Step 2 — Create the Vite config

Create a vite.config.ts (yes, even in a JS-only project — Vite's config uses TypeScript natively) at the project root:

vite.config.ts
import { defineConfig } from "vite";
import { vinext } from "vinext/vite";
import { cloudflare } from "@cloudflare/vite-plugin";
import { rsc } from "@vitejs/plugin-rsc";

export default defineConfig({
  plugins: [
    vinext({
      // Tells vinext this is an App Router project
      appRouter: true,
    }),
    cloudflare(),
    rsc(),
  ],
});

This config does three things:

  1. vinext() — adapts Next.js conventions (file-based routing, layouts, metadata) for Vite.
  2. cloudflare() — configures the build output for Workers and handles the workerd runtime.
  3. rsc() — enables React Server Components streaming through Vite's module graph.

Step 3 — Write the Worker entry

Create a worker/index.ts file. This is the entry point that Cloudflare Workers execute on every request:

worker/index.ts
import { handleRequest } from "vinext/server/app-router-entry";

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    return handleRequest(request, env, ctx);
  },
};

Tip. If you need image optimization at the edge, you can integrate Cloudflare Images here by passing the binding through env. For a basic deployment, the above is all you need.

Step 4 — Configure Wrangler

Create a wrangler.jsonc file at the project root:

wrangler.jsonc
{
  "name": "my-nextjs-blog",
  "compatibility_date": "2024-12-01",
  "assets": {
    "directory": "dist/client",
    "binding": "ASSETS"
  },
  "routes": [
    {
      "pattern": "blog.example.com",
      "custom_domain": true
    }
  ]
}

Key fields:

  • name — the Worker name visible in your Cloudflare dashboard.
  • compatibility_date — pins the Workers runtime to a specific API version.
  • assets — tells Wrangler to upload your static build output (dist/client) to Cloudflare's CDN.
  • routes — maps the Worker to your custom domain. Setting custom_domain: true lets Cloudflare manage DNS and SSL automatically.

Step 5 — Add build and deploy scripts

Update your package.json with these scripts:

package.json
{
  "scripts": {
    "dev": "next dev --turbopack",
    "dev:vinext": "npx vinext dev",
    "build": "next build",
    "build:vinext": "npx vinext build",
    "deploy": "npx vinext deploy"
  }
}
  • dev:vinext — runs a local Vite dev server that simulates the Workers environment with HMR.
  • build:vinext — produces the dist/ folder containing the Worker bundle and static assets.
  • deploy — runs build:vinext and then wrangler deploy in one step.

Step 6 — Deploy

With everything configured, deploying is a single command:

Terminal
npm run deploy

You should see output like:

useDebounce.js
✓ Built successfully
✓ Uploaded my-nextjs-blog (3.2s)
✓ Published to blog.example.com

Your Next.js app is now live on Cloudflare's edge network.

Setting up a custom domain

If you already have a domain on Cloudflare:

  1. Add the routes entry in wrangler.jsonc with custom_domain: true.
  2. Run npm run deploy — Cloudflare creates the DNS record and provisions an SSL certificate automatically.
  3. The domain should be active within a few minutes.

If your domain is registered elsewhere, first add it to Cloudflare by updating your registrar's nameservers to the ones Cloudflare provides. Once the domain is active in Cloudflare, the custom_domain: true approach works identically.

Environment variables

For local development, use .env.local:

.env.local
NEXT_PUBLIC_SITE_URL=http://localhost:3000
STRAPI_API_URL=http://localhost:1337
STRAPI_API_TOKEN=your-dev-token

For production, set secrets via Wrangler:

Terminal
npx wrangler secret put STRAPI_API_TOKEN

You can also define non-secret variables in wrangler.jsonc:

json
{
  "vars": {
    "NEXT_PUBLIC_SITE_URL": "https://blog.example.com"
  }
}

Important. NEXT_PUBLIC_* variables are inlined at build time by Next.js. They are NOT read from Workers environment variables at runtime. Make sure they are set before running build:vinext.

CI/CD with GitHub Actions

Automate deployments on every push to main:

Automated GitHub Actions CI/CD Pipeline to Cloudflare Workers
Automated GitHub Actions CI/CD Pipeline to Cloudflare Workers

.github/workflows/deploy.yml
name: Deploy to Cloudflare Workers

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci

      - run: npm run deploy
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

Create the secrets in your GitHub repo settings:

  1. CLOUDFLARE_API_TOKEN — generate one at dash.cloudflare.com/profile/api-tokens with the "Edit Cloudflare Workers" template.
  2. CLOUDFLARE_ACCOUNT_ID — find it on your Cloudflare dashboard overview page.

Windows compatibility

If you develop on Windows, you might encounter path-separator issues in vinext's Wrangler runner. The fix is to use patch-package:

Terminal
npm install patch-package --save-dev

Add a postinstall script:

json
{
  "scripts": {
    "postinstall": "patch-package"
  }
}

Then create the patch file patches/vinext+0.0.7.patch to normalize path separators. This is applied automatically on npm install for all contributors.

Troubleshooting common issues

"Module not found" errors during build

vinext resolves modules differently from the standard Next.js build. If you see missing module errors:

  • Make sure all dependencies are in dependencies, not devDependencies — Workers bundles need them at build time.
  • Check that any next.config.js transpilation settings are compatible with Vite.

Cold start latency spikes

Workers cold starts are typically under 50ms, but a very large bundle can increase this. Keep your Worker bundle lean:

  • Use Server Components for data-heavy pages (they stream HTML, not JS).
  • Avoid importing heavy libraries in the Worker entry point.
  • Use dynamic import() for rarely-used server-side code.

Static assets return 404

Make sure the assets.directory in wrangler.jsonc points to dist/client (the Vite build output for client assets). If you changed the Vite output directory, update this path to match.

Custom domain not resolving

After deploying with custom_domain: true:

  • Allow 5-10 minutes for DNS propagation.
  • Verify the domain is in "Active" status on your Cloudflare dashboard.
  • Check that no conflicting DNS records (like an existing A record) interfere with the Workers route.

Performance tips

Once deployed, squeeze out extra performance:

  1. Enable Smart Placement — lets Cloudflare run your Worker closer to your data source when it makes sense.
  2. Use Cache API — cache rendered HTML at the edge for pages that don't change often.
  3. Leverage stale-while-revalidate — serve cached content while revalidating in the background.
  4. Compress assets — Cloudflare automatically serves Brotli-compressed assets, but make sure your images are optimized before upload.

Wrapping up

Deploying Next.js to Cloudflare Workers is not the default path, but it is an incredibly powerful one. You get server-rendered React at the edge, sub-50ms cold starts, automatic scaling, and a global CDN — all on a generous free tier.

The key pieces are:

  • vinext to bridge Next.js App Router to Vite to Workers
  • wrangler.jsonc for Worker configuration and custom domains
  • GitHub Actions for automated deployments

Once the pipeline is set up, shipping is just git push.