fix(templates): don't use remote bindings in cloudflare template when developing locally (#14063)

### What?
Fixes the remote binding behavior so that they're only used when
deployed or when applying the database migrations.
I've also pinned the Wrangler version to prevent it breaking due to
behavior changes in minor versions.
And I took the opportunity to update the compatibility date, to one that
already includes the MessagePort by default.

### Why?
It turns out the remote binding behavior slightly changed since the beta
until the final release.
As a result, when developing locally, wrangler would connect to the
remote database, which is a big nono

### How?
By making sure the getCloudflareContext() method is not invoked outside
of NODE_ENV === 'production', as the wrangler.jsonc has the "remote"
flag as true for the D1 database and therefore will always point to the
remote database.

Fixes #14041

Co-authored-by: Ricardo Tavares <rtavares@cloudflare.com>
This commit is contained in:
Ricardo Tavares
2025-10-03 19:13:07 +01:00
committed by GitHub
parent ca3f054041
commit 9d3e54088f
4 changed files with 10 additions and 18 deletions

View File

@@ -1,7 +1,4 @@
import { withPayload } from '@payloadcms/next/withPayload'
import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'
initOpenNextCloudflareForDev({ environment: process.env.CLOUDFLARE_ENV })
/** @type {import('next').NextConfig} */
const nextConfig = {

View File

@@ -56,7 +56,7 @@
"typescript": "5.7.3",
"vite-tsconfig-paths": "5.1.4",
"vitest": "3.2.3",
"wrangler": "^4.26.1"
"wrangler": "~4.42.0"
},
"engines": {
"node": "^18.20.2 || >=20.9.0",
@@ -75,10 +75,5 @@
"description": "Generate a random string using `openssl rand -hex 32`"
}
}
},
"overrides": {
"@payloadcms/db-d1-sqlite": {
"drizzle-kit": "0.30.6"
}
}
}

View File

@@ -15,9 +15,11 @@ import { Media } from './collections/Media'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const cloudflare = process.argv.find((value) => value.match(/^(generate|migrate):?/))
? await getCloudflareContextFromWrangler()
: await getCloudflareContext({ async: true })
const cloudflareRemoteBindings = process.env.NODE_ENV === 'production'
const cloudflare =
process.argv.find((value) => value.match(/^(generate|migrate):?/)) || !cloudflareRemoteBindings
? await getCloudflareContextFromWrangler()
: await getCloudflareContext({ async: true })
export default buildConfig({
admin: {
@@ -47,7 +49,7 @@ function getCloudflareContextFromWrangler(): Promise<CloudflareContext> {
return import(`${'__wrangler'.replaceAll('_', '')}`).then(({ getPlatformProxy }) =>
getPlatformProxy({
environment: process.env.CLOUDFLARE_ENV,
experimental: { remoteBindings: process.env.NODE_ENV === 'production' },
experimental: { remoteBindings: cloudflareRemoteBindings },
} satisfies GetPlatformProxyOptions),
)
}

View File

@@ -2,7 +2,7 @@
"$schema": "node_modules/wrangler/config-schema.json",
"main": ".open-next/worker.js",
"name": "my-app",
"compatibility_date": "2025-05-05",
"compatibility_date": "2025-08-15",
"compatibility_flags": [
// Enable Node.js API
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag
@@ -10,8 +10,6 @@
// Allow to fetch URLs in your app
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-public
"global_fetch_strictly_public",
// Enable MessagePort, used by undici
"expose_global_message_channel",
],
"assets": {
"directory": ".open-next/assets",
@@ -45,7 +43,7 @@
],
// Here's how to configure an additional environment
// It can be deployed with `CLOUDFLARE_ENV=staging npm run deploy`
// It can be deployed with `CLOUDFLARE_ENV=staging pnpm run deploy`
// "env": {
// "staging": {
// "name": "my-app-staging",
@@ -54,7 +52,7 @@
// "binding": "D1",
// "database_id": "DATABASE_ID",
// "database_name": "my-app-staging",
// "experimental_remote": true
// "remote": true
// }
// ],
// "services": [