$ ls ./menu

© 2025 ESSA MAMDANI

cd ../blog
9 min read
Web Development

The Edge Awaits: A World-Class Guide to Migrating Your Vercel Projects to Cloudflare Pages & Workers

> Unlock unparalleled performance and flexibility. This comprehensive guide, crafted by an elite developer, walks you through every step of migrating your web projects from Vercel to Cloudflare Pages and Workers, ensuring a seamless transition and zero downtime.

Audio version coming soon
The Edge Awaits: A World-Class Guide to Migrating Your Vercel Projects to Cloudflare Pages & Workers
Verified by Essa Mamdani

Alright, fellow tech enthusiasts. Essa here, and today we're tackling a migration that's becoming increasingly relevant in our pursuit of ultimate performance, cost-efficiency, and edge-native architecture: moving your carefully crafted projects from Vercel to the expansive, powerful ecosystem of Cloudflare Pages and Workers.

Vercel has been a fantastic platform for many, myself included, offering a superb developer experience for deploying frontend frameworks and serverless functions. But as our applications scale, as the need for true edge compute becomes paramount, and as we look to consolidate our infrastructure under a single, robust roof, Cloudflare emerges as an incredibly compelling alternative. With its global network, powerful Workers platform, and increasingly mature Pages offering, it’s not just a hosting provider; it's a complete internet operating system.

This isn't just about changing providers; it's about embracing a new paradigm of web development that puts your code closer to your users than ever before. We're going to dive deep, cover every crucial detail, and ensure your migration is not just successful, but exemplary.

Why Make the Leap? Vercel vs. Cloudflare at the Edge

Before we get our hands dirty, let’s quickly frame why you might consider this migration. Both Vercel and Cloudflare Pages offer Git-based deployments for frontend applications and serverless functions. However, their underlying philosophies and ecosystems differ significantly:

  • Vercel: Often lauded for its Next.js integration and developer experience, Vercel excels at abstracting away infrastructure. Its serverless functions run on AWS Lambda.
  • Cloudflare: Built on its global network, Cloudflare Pages leverages this for frontend hosting, while Cloudflare Workers provide a unique, highly performant serverless environment running on V8 isolates at the edge, literally milliseconds away from your users. This often translates to lower latency and, for many use cases, more predictable costs at scale.

Migrating to Cloudflare often means:

  1. True Edge Compute: Workers run everywhere, not just in a few regions.
  2. Consolidated Infrastructure: DNS, CDN, security, serverless, and static hosting all under one Cloudflare dashboard.
  3. Cost Predictability & Efficiency: Cloudflare's pricing model, especially for Workers, can be incredibly attractive for high-volume applications.
  4. Advanced Edge Capabilities: Leverage Workers for complex routing, A/B testing, custom caching, and even AI inference directly at the edge.

Ready to unlock the full potential of the edge? Let's get started.

Phase 1: Setting Up Your Project on Cloudflare Pages

The first step is to get your static or single-page application (SPA) deployed on Cloudflare Pages. This is remarkably straightforward.

1. Connect Your Git Repository

Cloudflare Pages integrates seamlessly with GitHub, GitLab, and Bitbucket.

  • Navigate to the Cloudflare Dashboard.
  • Select "Pages" from the left sidebar.
  • Click "Create a project" and then "Connect Git".
  • Authorize Cloudflare to access your repository provider.
  • Select the repository you wish to migrate.

2. Configure Your Build Settings

This is where you tell Cloudflare Pages how to build your application. It's often very similar to your Vercel configuration.

  • Framework Preset: Cloudflare Pages offers presets for popular frameworks like Next.js (static export), React, Vue, Svelte, Astro, etc. Choose the one that matches your project.
  • Build Command: This is the command that compiles your application. For most projects, this will be npm run build, yarn build, or pnpm build.
  • Build Output Directory: This is the folder where your compiled static assets reside after the build command runs. Common examples include build, dist, public, or .next (for Next.js static export).

Example package.json scripts:

json
1// package.json
2{
3  "name": "my-awesome-app",
4  "version": "0.1.0",
5  "private": true,
6  "scripts": {
7    "dev": "next dev", // Vercel's dev command, not used by Pages build
8    "build": "next build && next export", // This is what Cloudflare Pages needs for static export
9    "start": "next start"
10  },
11  "dependencies": {
12    // ...
13  }
14}

In this Next.js example, your Build command would be npm run build (or pnpm build, yarn build), and your Build output directory would be out (the default for next export).

Pro Tip: If your Vercel project uses a monorepo, Cloudflare Pages supports specifying a Root directory to point to the specific project within the monorepo that you want to deploy.

Phase 2: Migrating Your Serverless Functions (Workers)

This is often the most significant part of the migration, as Vercel's Serverless Functions (Node.js on AWS Lambda) have a different execution model and API surface compared to Cloudflare Workers (JavaScript/TypeScript on V8 isolates).

1. Understanding the Differences

  • Vercel Functions: Node.js environment, req/res objects similar to Express, typically deployed to specific AWS regions.
  • Cloudflare Workers: Service Worker API-like event.request and event.respondWith(new Response(...)), executed globally at the edge. No Node.js built-in modules directly available (e.g., fs, path), but many polyfills and edge-native alternatives exist.

2. Rewriting Your API Routes/Functions

You'll need to rewrite your Vercel API routes to the Cloudflare Workers syntax.

Vercel API Route Example (api/hello.js):

javascript
1// api/hello.js (Vercel)
2export default function handler(req, res) {
3  if (req.method === 'GET') {
4    res.status(200).json({ message: 'Hello from Vercel!' });
5  } else {
6    res.status(405).json({ message: 'Method Not Allowed' });
7  }
8}

Cloudflare Worker Example (worker.js or directly in Pages Functions functions/api/hello.js):

javascript
1// functions/api/hello.js (Cloudflare Pages Functions)
2export async function onRequestGet({ request, env }) {
3  return new Response(JSON.stringify({ message: 'Hello from Cloudflare Workers!' }), {
4    headers: { 'Content-Type': 'application/json' },
5  });
6}
7
8// Or a standalone Worker
9// export default {
10//   async fetch(request, env, ctx) {
11//     const url = new URL(request.url);
12//     if (request.method === 'GET' && url.pathname === '/api/hello') {
13//       return new Response(JSON.stringify({ message: 'Hello from Cloudflare Workers!' }), {
14//         headers: { 'Content-Type': 'application/json' },
15//       });
16//     }
17//     return new Response('Not Found', { status: 404 });
18//   },
19// };

Cloudflare Pages Functions (also known as "Functions") simplify this by allowing you to place Worker-compatible files directly in a functions directory in your Pages project root. They automatically map to routes.

3. State and Storage Migration

If your Vercel functions relied on external databases or storage, you'll continue to use them. However, if you used Vercel's ephemeral disk or specific integrations, consider Cloudflare's native storage solutions:

  • KV (Key-Value) Storage: For simple, fast, global key-value data. Excellent for caching, feature flags, or user session data.
  • Durable Objects: For stateful serverless, persistent WebSocket connections, or coordinating distributed systems. This is a game-changer for many architectures.
  • R2 (Object Storage): S3-compatible object storage, but without egress fees. Perfect for storing user uploads, media files, or large datasets accessed by Workers.

Pro Tip: Use the wrangler CLI for local development and deployment of standalone Workers. It provides a fantastic development experience, including local testing with wrangler dev. For Pages Functions, local development is often integrated with your framework's dev server.

Phase 3: Migrating Custom Domains with Zero Downtime

Maintaining your domain and ensuring a smooth transition is critical.

1. Add Custom Domains to Cloudflare Pages

  • In your Cloudflare Pages project settings, navigate to "Custom domains".
  • Click "Set up a custom domain" and enter your domain (e.g., example.com or www.example.com).
  • Cloudflare will guide you through verifying the domain, often by adding CNAME or TXT records.

2. Update DNS Records

If your domain is already managed by Cloudflare DNS (highly recommended for Pages/Workers), this is incredibly simple:

  • For your root domain (example.com), create an A record pointing to 104.18.21.60 and 104.18.20.60 (Cloudflare's default IP addresses for Pages).
  • For subdomains (www.example.com), create a CNAME record pointing to your Pages project's .pages.dev URL (e.g., my-project.pages.dev).

If your DNS is currently managed by Vercel or another provider:

  1. Change Nameservers: The most robust way is to change your domain's nameservers at your registrar to Cloudflare's nameservers. This brings your entire DNS management into Cloudflare.
  2. Add DNS Records: Once Cloudflare is managing your DNS, add the A and CNAME records as described above.

Zero Downtime Strategy:

  1. Deploy to Pages first: Get your entire application (frontend + functions) working perfectly on your .pages.dev URL.
  2. Test thoroughly: Ensure everything functions as expected.
  3. Update DNS: Only then update your custom domain's DNS records to point to Cloudflare Pages. Cloudflare's CDN will start serving your new deployment almost instantly.

Pro Tip: If you're using Cloudflare for DNS already, ensure the orange cloud (proxying) is enabled for your Pages-related DNS records. This activates Cloudflare's CDN, WAF, and DDoS protection.

Phase 4: Redirects, Headers, and Edge Configuration

Vercel uses vercel.json for redirects, headers, and other routing logic. Cloudflare Pages has its own mechanisms.

1. Redirects

Cloudflare Pages uses a _redirects file in your build output directory.

Vercel vercel.json redirect example:

json
1// vercel.json
2{
3  "redirects": [
4    {
5      "source": "/old-path",
6      "destination": "/new-path",
7      "permanent": true
8    }
9  ]
10}

Cloudflare Pages _redirects example:

# public/_redirects (or build output dir)
/old-path /new-path 301
/blog/* /articles/:splat 302
/legacy-page /new-page

2. Custom Headers

Similarly, Cloudflare Pages uses a _headers file.

Vercel vercel.json headers example:

json
1// vercel.json
2{
3  "headers": [
4    {
5      "source": "/(.*)",
6      "headers": [
7        {
8          "key": "X-Frame-Options",
9          "value": "DENY"
10        }
11      ]
12    }
13  ]
14}

Cloudflare Pages _headers example:

# public/_headers
/*
  X-Frame-Options: DENY
  X-XSS-Protection: 1; mode=block
  Cache-Control: public, max-age=31536000, immutable

3. Advanced Edge Logic with Workers

For more complex routing, URL rewrites, A/B testing, geo-targeting, or integrating with other Cloudflare services, you can leverage a full-fledged Cloudflare Worker deployed in front of your Pages project. This allows for unparalleled flexibility.

Pro Tip: Cloudflare Workers can intercept requests before they even hit your Pages application, allowing you to implement powerful edge logic, authentication, or even serve entirely different content based on custom rules.

Phase 5: Environment Variables & Secrets

Managing secrets and configuration is crucial for any application.

  • Cloudflare Pages: Go to your project settings, then "Environment variables". You can add both build-time and runtime environment variables.
  • Cloudflare Workers: For standalone Workers, you define environment variables and secrets via the wrangler.toml file or directly in the Cloudflare dashboard. Secrets are securely encrypted.

Pro Tip: Never commit sensitive information directly to your Git repository. Always use Cloudflare's secure environment variable management.

Phase 6: Testing and Validation

A successful migration isn't just about deployment; it's about ensuring everything works flawlessly.

  1. Test on .pages.dev: Before pointing your custom domain, thoroughly test your application on the Cloudflare-provided .pages.dev URL. Check all routes, API endpoints, forms, and third-party integrations.
  2. Monitor: Once your custom domain is live, monitor your application's performance, error rates, and logs (available in the Cloudflare dashboard) closely.
  3. Rollback Plan: Always have a rollback plan. If issues arise, you should be able to quickly revert your DNS to point back to Vercel (if you haven't fully decommissioned it yet).

Embracing the Cloudflare Ecosystem: Advanced Considerations

Once you're on Cloudflare, you're not just getting hosting; you're gaining access to a vast array of services at the edge:

  • Cloudflare R2: For S3-compatible object storage without egress fees. A fantastic alternative to AWS S3 or other storage solutions, especially for media or large files.
  • Cloudflare D1: Serverless SQL database, built on SQLite, running at the edge. A powerful new option for persistent data.
  • Cloudflare Stream: Video storage and delivery optimized for the edge.
  • Cloudflare Access: Securely authenticate and authorize users to your applications or internal tools without a VPN.
  • Analytics: Cloudflare provides detailed analytics on traffic, threats, and performance.

These services can help you further optimize, secure, and scale your application, taking full advantage of the global edge network.

The Future is Edge-Native

Migrating from Vercel to Cloudflare Pages and Workers is more than just a change of providers; it's a strategic move towards a truly edge-native architecture. It empowers you to build faster, more resilient, and more cost-effective applications that leverage the power of a global network.

The learning curve for Workers can be slightly steeper if you're coming from a purely Node.js serverless background, but the performance, flexibility, and cost benefits are often well worth the investment. With this guide, you have the blueprint to make that transition seamless and successful.

Now, go forth and build something incredible at the edge!