Get the FREE Ultimate OpenClaw Setup Guide →
j

Vercel to Cloudflare Worker Migration

Verified

@jiafar

npx machina-cli add skill @jiafar/vercel-to-cloudflare --openclaw
Files (1)
SKILL.md
3.9 KB

Vercel to Cloudflare Worker Migration

Migrate a Next.js + Supabase project from Vercel to Cloudflare Workers with Hyperdrive connection pooling.

Quick Start

  1. Run the analysis script to scan the project:
    python3 scripts/analyze_project.py <project-path>
    
  2. Review the migration report
  3. Run the migration script:
    python3 scripts/migrate.py <project-path>
    
  4. Configure Hyperdrive: see references/hyperdrive-setup.md

Core Migration Steps

1. Install @opennextjs/cloudflare adapter

npm install @opennextjs/cloudflare

Update next.config.js or next.config.ts if needed.

2. Rewrite environment variable access

All process.env.XXX for Cloudflare bindings (Hyperdrive, KV, D1, etc.) must use getCloudflareContext():

// BEFORE (Vercel/Node.js)
const url = process.env.DATABASE_URL;

// AFTER (Cloudflare Worker)
import { getCloudflareContext } from '@opennextjs/cloudflare';

function getConnectionInfo() {
  const env = getCloudflareContext().env;
  const hyperdrive = env.HYPERDRIVE as { connectionString?: string } | undefined;
  if (hyperdrive?.connectionString) {
    return { connectionString: hyperdrive.connectionString, source: 'hyperdrive' };
  }
  // Fallback for local dev
  const local = env.CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE;
  if (local) {
    return { connectionString: local, source: 'hyperdrive-local' };
  }
  throw new Error('HYPERDRIVE is not configured');
}

3. Refactor global DB singleton to per-request pattern

// BEFORE: Global singleton
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
const client = postgres(process.env.DATABASE_URL!);
export const db = drizzle(client);

// AFTER: Per-request with React cache
import { cache } from 'react';

export const getDb = cache(() => {
  const { connectionString, source } = getConnectionInfo();
  return createDatabase({
    connectionString,
    enableSSL: source === 'hyperdrive' ? false : 'require',
  });
});

Then replace all import { db } with import { getDb } and add const db = getDb() at the start of each function.

4. Configure wrangler.toml

name = "my-app"
main = ".open-next/worker.js"
compatibility_date = "2024-09-23"
compatibility_flags = ["nodejs_compat"]

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<your-hyperdrive-id>"

Critical Pitfalls

  1. Hyperdrive must connect to Supabase Direct Connection (port 5432), NOT the Pooler (port 6543). Hyperdrive IS a connection pooler — connecting pooler-to-pooler causes errors.

  2. SSL must be disabled for Hyperdrive connections — Worker ↔ Hyperdrive is internal network. Only enable SSL for direct database connections (local dev, build stage).

  3. Cannot initialize DB at module top levelgetCloudflareContext() only works during request handling, not at module load time.

  4. Supabase Free Tier direct connection is IPv6 only — local dev may fail if your network doesn't support IPv6. Use the Pooler URL (port 6543) for local development.

Detailed References

Source

git clone https://clawhub.ai/jiafar/vercel-to-cloudflareView on GitHub

Overview

Migrate a Next.js + Supabase project from Vercel to Cloudflare Workers with Hyperdrive connection pooling. This guide helps you reduce costs, deploy at the edge, and resolve Supabase connectivity issues on Cloudflare.

How This Skill Works

The migration uses the @opennextjs/cloudflare adapter, rewrites environment access to use getCloudflareContext(), and shifts database access to a per-request pattern. It also configures wrangler.toml with a Hyperdrive binding to enable Cloudflare–Supabase connectivity while preserving code behavior.

When to Use It

  • You want to reduce hosting costs by moving a Next.js app off Vercel.
  • You need to deploy a Next.js app on Cloudflare Workers with edge-first hosting.
  • You require Hyperdrive connection pooling for Supabase on Cloudflare.
  • You encounter Supabase connectivity issues after moving to Cloudflare and need direct connections.
  • You want an end-to-end migration workflow using analyze and migrate scripts.

Quick Start

  1. Step 1: Run the analysis script to scan the project: python3 scripts/analyze_project.py <project-path>
  2. Step 2: Review the migration report
  3. Step 3: Run the migration script: python3 scripts/migrate.py <project-path>

Best Practices

  • Install the @opennextjs/cloudflare adapter and update next.config.js/ts as needed.
  • Rewrite env access to getCloudflareContext() for Cloudflare-bindings (Hyperdrive, KV, D1, etc.).
  • Refactor database access to a per-request pattern; avoid module-level DB singletons.
  • Configure wrangler.toml with a Hyperdrive binding (HYPERDRIVE) and correct IDs.
  • Ensure Hyperdrive connects to Supabase directly (port 5432) and disable SSL for internal traffic.

Example Use Cases

  • A startup migrates a Next.js app from Vercel to Cloudflare Workers to cut monthly costs while keeping Supabase integration.
  • A developer deploys a Next.js app on Cloudflare Workers using Hyperdrive connection pooling to handle spikes in user traffic.
  • An engineering team fixes Supabase connectivity issues post-migration by routing Hyperdrive through a direct Postgres connection (5432).
  • An engineer runs the analyze and migrate scripts to generate a Cloudflare-ready migration plan for a large Next.js app.
  • The team updates env patterns and next.config to rely on getCloudflareContext for Cloudflare-bound bindings.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers