Skip to content

Commit 462b954

Browse files
committed
fix: remove env when no CLOUDFLARE_ENV set
1 parent 8161ac8 commit 462b954

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

docs/content/docs/1.getting-started/3.deploy.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,48 @@ Then make sure to create the following `wrangler.jsonc` file in the root of your
6868
}
6969
```
7070

71+
### Environments
72+
73+
Wrangler supports [environments](https://developers.cloudflare.com/workers/wrangler/environments/) to manage different configurations for staging, preview, or other deployment targets. You can define environment-specific bindings in your `wrangler.jsonc`:
74+
75+
```jsonc [wrangler.jsonc]
76+
{
77+
"$schema": "node_modules/wrangler/config-schema.json",
78+
// Production bindings (top-level)
79+
"d1_databases": [
80+
{
81+
"binding": "DB",
82+
"database_id": "<production-database-id>"
83+
}
84+
],
85+
// Preview environment
86+
"env": {
87+
"preview": {
88+
"d1_databases": [
89+
{
90+
"binding": "DB",
91+
"database_id": "<preview-database-id>"
92+
}
93+
]
94+
}
95+
}
96+
}
97+
```
98+
99+
To deploy to a specific environment, set the `CLOUDFLARE_ENV` environment variable during build:
100+
101+
```bash [Terminal]
102+
CLOUDFLARE_ENV=preview nuxt build
103+
```
104+
105+
::note
106+
Bindings such as `d1_databases`, `kv_namespaces`, `r2_buckets`, and `vars` are non-inheritable and must be specified in each environment.
107+
::
108+
109+
::callout{to="https://developers.cloudflare.com/workers/wrangler/environments/"}
110+
Learn more about Wrangler environments in the Cloudflare documentation.
111+
::
112+
71113
### Cache
72114

73115
When self-hosting and using devtools for cache management (viewing/deleting cache entries), you need to configure additional environment variables:

src/hosting/cloudflare.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ async function processWranglerConfigFile(nuxt: Nuxt, targetEnv?: string) {
146146
const content = await readFile(wranglerPath, 'utf-8')
147147
let config: WranglerConfigWithEnv = JSON.parse(content)
148148

149-
// Process environment if CLOUDFLARE_ENV is set
150-
if (targetEnv) {
151-
if (!config.env || !config.env[targetEnv]) {
152-
log.info(`No environment "${targetEnv}" found in wrangler config, using top-level configuration`)
153-
// Still remove the env section if it exists
154-
if (config.env) {
155-
const { env: _, ...rest } = config
156-
config = rest
157-
}
158-
} else {
159-
// Process the config to flatten the environment
160-
config = processWranglerConfigEnv(config, targetEnv)
149+
if (targetEnv && config.env?.[targetEnv]) {
150+
// Flatten the specified environment into top-level config
151+
config = processWranglerConfigEnv(config, targetEnv)
152+
log.info(`Using wrangler environment \`${targetEnv}\``)
153+
} else {
154+
// Remove env section if present (use top-level config)
155+
if (config.env) {
156+
const { env: _, ...rest } = config
157+
config = rest
158+
}
159+
if (targetEnv) {
160+
log.info(`No environment \`${targetEnv}\` found in wrangler config, using top-level configuration`)
161161
}
162162
}
163163

@@ -166,10 +166,6 @@ async function processWranglerConfigFile(nuxt: Nuxt, targetEnv?: string) {
166166

167167
// Write the processed config back
168168
await writeFile(wranglerPath, JSON.stringify(config, null, 2), 'utf-8')
169-
170-
if (targetEnv) {
171-
log.success(`Processed wrangler config for environment "${targetEnv}"`)
172-
}
173169
} catch (error) {
174170
log.error(`Failed to process wrangler config: ${error}`)
175171
}

0 commit comments

Comments
 (0)