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:
@@ -1,7 +1,4 @@
|
|||||||
import { withPayload } from '@payloadcms/next/withPayload'
|
import { withPayload } from '@payloadcms/next/withPayload'
|
||||||
import { initOpenNextCloudflareForDev } from '@opennextjs/cloudflare'
|
|
||||||
|
|
||||||
initOpenNextCloudflareForDev({ environment: process.env.CLOUDFLARE_ENV })
|
|
||||||
|
|
||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
const nextConfig = {
|
const nextConfig = {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
"typescript": "5.7.3",
|
"typescript": "5.7.3",
|
||||||
"vite-tsconfig-paths": "5.1.4",
|
"vite-tsconfig-paths": "5.1.4",
|
||||||
"vitest": "3.2.3",
|
"vitest": "3.2.3",
|
||||||
"wrangler": "^4.26.1"
|
"wrangler": "~4.42.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^18.20.2 || >=20.9.0",
|
"node": "^18.20.2 || >=20.9.0",
|
||||||
@@ -75,10 +75,5 @@
|
|||||||
"description": "Generate a random string using `openssl rand -hex 32`"
|
"description": "Generate a random string using `openssl rand -hex 32`"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"overrides": {
|
|
||||||
"@payloadcms/db-d1-sqlite": {
|
|
||||||
"drizzle-kit": "0.30.6"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ import { Media } from './collections/Media'
|
|||||||
const filename = fileURLToPath(import.meta.url)
|
const filename = fileURLToPath(import.meta.url)
|
||||||
const dirname = path.dirname(filename)
|
const dirname = path.dirname(filename)
|
||||||
|
|
||||||
const cloudflare = process.argv.find((value) => value.match(/^(generate|migrate):?/))
|
const cloudflareRemoteBindings = process.env.NODE_ENV === 'production'
|
||||||
? await getCloudflareContextFromWrangler()
|
const cloudflare =
|
||||||
: await getCloudflareContext({ async: true })
|
process.argv.find((value) => value.match(/^(generate|migrate):?/)) || !cloudflareRemoteBindings
|
||||||
|
? await getCloudflareContextFromWrangler()
|
||||||
|
: await getCloudflareContext({ async: true })
|
||||||
|
|
||||||
export default buildConfig({
|
export default buildConfig({
|
||||||
admin: {
|
admin: {
|
||||||
@@ -47,7 +49,7 @@ function getCloudflareContextFromWrangler(): Promise<CloudflareContext> {
|
|||||||
return import(`${'__wrangler'.replaceAll('_', '')}`).then(({ getPlatformProxy }) =>
|
return import(`${'__wrangler'.replaceAll('_', '')}`).then(({ getPlatformProxy }) =>
|
||||||
getPlatformProxy({
|
getPlatformProxy({
|
||||||
environment: process.env.CLOUDFLARE_ENV,
|
environment: process.env.CLOUDFLARE_ENV,
|
||||||
experimental: { remoteBindings: process.env.NODE_ENV === 'production' },
|
experimental: { remoteBindings: cloudflareRemoteBindings },
|
||||||
} satisfies GetPlatformProxyOptions),
|
} satisfies GetPlatformProxyOptions),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"$schema": "node_modules/wrangler/config-schema.json",
|
"$schema": "node_modules/wrangler/config-schema.json",
|
||||||
"main": ".open-next/worker.js",
|
"main": ".open-next/worker.js",
|
||||||
"name": "my-app",
|
"name": "my-app",
|
||||||
"compatibility_date": "2025-05-05",
|
"compatibility_date": "2025-08-15",
|
||||||
"compatibility_flags": [
|
"compatibility_flags": [
|
||||||
// Enable Node.js API
|
// Enable Node.js API
|
||||||
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag
|
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag
|
||||||
@@ -10,8 +10,6 @@
|
|||||||
// Allow to fetch URLs in your app
|
// Allow to fetch URLs in your app
|
||||||
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-public
|
// see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#global-fetch-strictly-public
|
||||||
"global_fetch_strictly_public",
|
"global_fetch_strictly_public",
|
||||||
// Enable MessagePort, used by undici
|
|
||||||
"expose_global_message_channel",
|
|
||||||
],
|
],
|
||||||
"assets": {
|
"assets": {
|
||||||
"directory": ".open-next/assets",
|
"directory": ".open-next/assets",
|
||||||
@@ -45,7 +43,7 @@
|
|||||||
],
|
],
|
||||||
|
|
||||||
// Here's how to configure an additional environment
|
// 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": {
|
// "env": {
|
||||||
// "staging": {
|
// "staging": {
|
||||||
// "name": "my-app-staging",
|
// "name": "my-app-staging",
|
||||||
@@ -54,7 +52,7 @@
|
|||||||
// "binding": "D1",
|
// "binding": "D1",
|
||||||
// "database_id": "DATABASE_ID",
|
// "database_id": "DATABASE_ID",
|
||||||
// "database_name": "my-app-staging",
|
// "database_name": "my-app-staging",
|
||||||
// "experimental_remote": true
|
// "remote": true
|
||||||
// }
|
// }
|
||||||
// ],
|
// ],
|
||||||
// "services": [
|
// "services": [
|
||||||
|
|||||||
Reference in New Issue
Block a user