Migrate a Next.js + Supabase project from Vercel to Cloudflare Workers with Hyperdrive connection pooling.
```bash
python3 scripts/analyze_project.py
```
```bash
python3 scripts/migrate.py
```
npm install @opennextjs/cloudflare
Update next.config.js or next.config.ts if needed.
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');
}
// 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.
name = "my-app"
main = ".open-next/worker.js"
compatibility_date = "2024-09-23"
compatibility_flags = ["nodejs_compat"]
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<your-hyperdrive-id>"
getCloudflareContext() only works during request handling, not at module load time.共 1 个版本