Skip to content

Commit 9d3e540

Browse files
rjgtavRicardo Tavares
andauthored
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>
1 parent ca3f054 commit 9d3e540

File tree

4 files changed

+10
-18
lines changed

4 files changed

+10
-18
lines changed

templates/with-cloudflare-d1/next.config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { withPayload } from '@payloadcms/next/withPayload'
2-
import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'
3-
4-
initOpenNextCloudflareForDev({ environment: process.env.CLOUDFLARE_ENV })
52

63
/** @type {import('next').NextConfig} */
74
const nextConfig = {

templates/with-cloudflare-d1/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"typescript": "5.7.3",
5757
"vite-tsconfig-paths": "5.1.4",
5858
"vitest": "3.2.3",
59-
"wrangler": "^4.26.1"
59+
"wrangler": "~4.42.0"
6060
},
6161
"engines": {
6262
"node": "^18.20.2 || >=20.9.0",
@@ -75,10 +75,5 @@
7575
"description": "Generate a random string using `openssl rand -hex 32`"
7676
}
7777
}
78-
},
79-
"overrides": {
80-
"@payloadcms/db-d1-sqlite": {
81-
"drizzle-kit": "0.30.6"
82-
}
8378
}
8479
}

templates/with-cloudflare-d1/src/payload.config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { Media } from './collections/Media'
1515
const filename = fileURLToPath(import.meta.url)
1616
const dirname = path.dirname(filename)
1717

18-
const cloudflare = process.argv.find((value) => value.match(/^(generate|migrate):?/))
19-
? await getCloudflareContextFromWrangler()
20-
: await getCloudflareContext({ async: true })
18+
const cloudflareRemoteBindings = process.env.NODE_ENV === 'production'
19+
const cloudflare =
20+
process.argv.find((value) => value.match(/^(generate|migrate):?/)) || !cloudflareRemoteBindings
21+
? await getCloudflareContextFromWrangler()
22+
: await getCloudflareContext({ async: true })
2123

2224
export default buildConfig({
2325
admin: {
@@ -47,7 +49,7 @@ function getCloudflareContextFromWrangler(): Promise<CloudflareContext> {
4749
return import(`${'__wrangler'.replaceAll('_', '')}`).then(({ getPlatformProxy }) =>
4850
getPlatformProxy({
4951
environment: process.env.CLOUDFLARE_ENV,
50-
experimental: { remoteBindings: process.env.NODE_ENV === 'production' },
52+
experimental: { remoteBindings: cloudflareRemoteBindings },
5153
} satisfies GetPlatformProxyOptions),
5254
)
5355
}

templates/with-cloudflare-d1/wrangler.jsonc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
"$schema": "node_modules/wrangler/config-schema.json",
33
"main": ".open-next/worker.js",
44
"name": "my-app",
5-
"compatibility_date": "2025-05-05",
5+
"compatibility_date": "2025-08-15",
66
"compatibility_flags": [
77
// Enable Node.js API
88
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag
99
"nodejs_compat",
1010
// Allow to fetch URLs in your app
1111
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-public
1212
"global_fetch_strictly_public",
13-
// Enable MessagePort, used by undici
14-
"expose_global_message_channel",
1513
],
1614
"assets": {
1715
"directory": ".open-next/assets",
@@ -45,7 +43,7 @@
4543
],
4644

4745
// Here's how to configure an additional environment
48-
// It can be deployed with `CLOUDFLARE_ENV=staging npm run deploy`
46+
// It can be deployed with `CLOUDFLARE_ENV=staging pnpm run deploy`
4947
// "env": {
5048
// "staging": {
5149
// "name": "my-app-staging",
@@ -54,7 +52,7 @@
5452
// "binding": "D1",
5553
// "database_id": "DATABASE_ID",
5654
// "database_name": "my-app-staging",
57-
// "experimental_remote": true
55+
// "remote": true
5856
// }
5957
// ],
6058
// "services": [

0 commit comments

Comments
 (0)