$ ls ./menu

© 2025 ESSA MAMDANI

cd ../blog
5 min read
Web Development

From Vercel to Cloudflare: The Elite Guide to Migrating Your Frontend and Serverless Stack

> Stop paying the "Vercel Tax" and unlock the true power of the edge. This comprehensive guide covers migrating your Next.js and static applications to Cloudflare Pages and Workers for better performance and predictable pricing.

Audio version coming soon
From Vercel to Cloudflare: The Elite Guide to Migrating Your Frontend and Serverless Stack
Verified by Essa Mamdani

Let’s be real: Vercel is phenomenal for Developer Experience (DX). They’ve mastered the "it just works" workflow. But as your traffic scales, you eventually hit the wall—the dreaded "Vercel Tax." Whether it’s the $40/month per seat or the eye-watering bandwidth costs, there comes a time in every elite developer’s journey where you want more control, better pricing, and a closer proximity to the metal.

Cloudflare isn’t just a CDN anymore; it’s a full-stack powerhouse. By migrating to Cloudflare Pages and Workers, you aren’t just moving hosts—you’re deploying your code directly to the edge of the internet.

In this guide, I’ll walk you through the architectural shift, the technical hurdles, and the pro-level optimizations you need to move your stack from Vercel to Cloudflare.


1. The Architectural Shift: Pages vs. Workers

On Vercel, the distinction between static assets and Serverless Functions is largely abstracted away. On Cloudflare, you have two primary primitives:

  • Cloudflare Pages: Optimized for frontend frameworks and static sites. It handles the CI/CD, preview deployments, and asset hosting.
  • Cloudflare Workers: The serverless compute platform. If you’re building a heavy API or need complex middleware, this is your engine.

Pro Tip: For most modern apps (Next.js, Nuxt, Remix), Cloudflare Pages now supports "Functions" out of the box, which are essentially Workers running under the hood. You get the best of both worlds.


2. Migrating Next.js: The Edge Runtime Challenge

Vercel is the home of Next.js, so moving it away requires a bit of finesse. Cloudflare uses the Edge Runtime (based on V8), not Node.js. This means you cannot use certain Node.js APIs (like fs or net).

To bridge this gap, we use @cloudflare/next-on-pages.

Step 1: Install the adapter

bash
1npm install --save-dev @cloudflare/next-on-pages

Step 2: Configure your Next.js app

You must force your pages to use the edge runtime. Add this to your page or route handler:

typescript
1export const runtime = 'edge';
2
3export default async function Page() {
4  // Your logic here
5}

Step 3: The Build Process

Instead of the standard next build, you’ll run the adapter to transform the Next.js output into a format Cloudflare understands.

bash
1npx @cloudflare/next-on-pages

3. Environment Variables and Secrets

In Vercel, you’re used to the web dashboard for environment variables. While Cloudflare has a dashboard, elite devs live in the CLI.

Cloudflare differentiates between Environment Variables (public-ish build time vars) and Secrets (encrypted at rest, for API keys).

Using wrangler, Cloudflare’s CLI, you can manage these easily:

bash
1# Adding a secret for your production environment
2npx wrangler pages secret put STRIPE_API_KEY

Insider Knowledge: Unlike Vercel, where environment variables are available globally in your functions, Cloudflare Workers/Pages Functions receive variables via the env object in the fetch handler. If you're using the next-on-pages adapter, it shims this for you, but keep an eye on your variable scopes.


4. Handling Data: From Vercel KV/Postgres to Cloudflare D1/Hyperdrive

If you’re using Vercel’s storage solutions, you’re likely using their wrappers around Upstash (KV) or Neon (Postgres). Cloudflare has native equivalents that are often faster because they reside on the same global network as your compute.

  • Vercel KV -> Cloudflare KV: High-read, low-write key-value storage.
  • Vercel Postgres -> Cloudflare Hyperdrive + Your DB: Hyperdrive makes your existing regional database feel like it's global by pooling connections and caching queries at the edge.
  • Vercel Blob -> Cloudflare R2: R2 is S3-compatible and has zero egress fees. This is a massive cost saver.

Example: Accessing R2 in a Cloudflare Function

typescript
1export interface Env {
2  MY_BUCKET: R2Bucket;
3}
4
5export default {
6  async fetch(request: Request, env: Env): Promise<Response> {
7    const object = await env.MY_BUCKET.get('user-report.pdf');
8    if (object === null) return new Response('Not Found', { status: 404 });
9    
10    return new Response(object.body);
11  },
12};

5. Routing and Redirects

Vercel uses vercel.json or next.config.js for redirects. In Cloudflare Pages, you use a simple _redirects file placed in your build output directory.

Create a public/_redirects file:

text
1/old-blog/*  /new-blog/:splat  301
2/api/*       https://api.myapp.com/:splat  200

For more complex logic, you’ll want to use Cloudflare Workers Bulk Redirects or write a simple Worker to handle the logic. The performance difference is negligible, but the control is absolute.


6. The "Wrangler" Workflow

To truly master Cloudflare, you need to embrace wrangler.toml. This configuration file acts as the source of truth for your infrastructure.

toml
1name = "my-awesome-app"
2pages_build_output_dir = ".vercel/output/static"
3
4[[kv_namespaces]]
5binding = "CACHE"
6id = "xxxxxxxxxxxxxxxxxxxxxxxx"
7
8[[r2_buckets]]
9binding = "UPLOADS"
10bucket_name = "user-uploads"

Running npx wrangler pages dev allows you to emulate the entire Cloudflare environment locally, including KV, D1, and R2. This is significantly more robust than Vercel’s local dev server when it comes to edge-specific features.


7. The Migration Checklist: Final Steps

  1. DNS Transfer: You don’t have to use Cloudflare for DNS to use Pages, but you should. It enables CNAME flattening (allowing you to use Pages on a root domain) and superior WAF protection.
  2. Image Optimization: Vercel Image Optimization is expensive. Switch to Cloudflare Images or use a Worker to proxy and resize images on the fly.
  3. Analytics: Swap Vercel Analytics for Cloudflare Web Analytics. It’s privacy-focused, free, and doesn’t require a client-side script if you enable it at the edge level.
  4. Middleware: If you have complex middleware.ts in Next.js, ensure it doesn't use Node-specific libraries. Test it thoroughly using wrangler.

Summary: Why the Move Matters

Migrating from Vercel to Cloudflare is a rite of passage for developers who want to own their infrastructure without the overhead of managing raw VMs. You gain:

  • Performance: Your code runs in 300+ cities globally, not just a handful of AWS regions.
  • Cost Efficiency: No more bandwidth anxiety. R2 and Workers pricing models are significantly more generous for high-scale apps.
  • Unification: DNS, Security, Compute, and Storage all managed under one roof.

Vercel is a great starting point, but Cloudflare is where you build the future. See you at the edge.