You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This entry describe the most sensible way to handle your environment variables which works well both during local development and once your application is deployed to Cloudflare Workers.
4
+
5
+
On the Cloudflare platform, your environment variables can be stored in either ["Enviroment variables"](https://developers.cloudflare.com/workers/configuration/environment-variables/) or ["Secrets"](https://developers.cloudflare.com/workers/configuration/secrets/). The difference being that Secrets are write only and can not be read back from either the dashboard or the CLI.
6
+
7
+
### Local development
8
+
9
+
While there are multiple ways to set environment variables for local development on the Cloudflare platform (adding them to to your [wrangler configuration](https://developers.cloudflare.com/workers/configuration/secrets/) or to a [.dev.vars](https://developers.cloudflare.com/workers/configuration/secrets/) file) that does not play well with the recommended development workflow as they would not be available while using `next dev`.
10
+
11
+
What you should do instead is to use the Next.js [`.env` files](https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables) instead. By doing so the environment variables will be available on `process.env` both while running `next dev` and when running you app locally on a Worker with `wrangler dev`.
12
+
13
+
Next.js `.env` files are environment specific. That is a `.env.development` will take precedence over a `.env` file when you use the "development" environment. See the Next.js site for a detailed explanation of the [loading order](https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables).
14
+
15
+
You should use the `NEXTJS_ENV` environment variable to select the environment to use when running your app locally on a worker, that's how you would select the "development" environment:
16
+
17
+
```plain
18
+
# .dev.vars
19
+
NEXTJS_ENV=development
20
+
```
21
+
22
+
The "production" environment is used by default when `NEXTJS_ENV` is not explicitely set.
23
+
24
+
### Production
25
+
26
+
`.env` and `dev.vars` are local files that should not be added to source control. You should instead use the cloudflare dashboard to set you environment variables for production.
27
+
28
+
Next.js has [2 kinds](https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser) of environment variables:
29
+
30
+
- non-`NEXT_PUBLIC_` variables which are only available on the server
31
+
-`NEXT_PUBLIC_` variables on the other hand are available to the browser
32
+
33
+
Those are called runtime environment variables (non-`NEXT_PUBLIC_`) and buildtime environment variables (`NEXT_PUBLIC_`) on the Cloudflare paltform.
34
+
35
+
You can set the runtime environment variables (non-`NEXT_PUBLIC_`) by following those [instructions](https://developers.cloudflare.com/workers/configuration/environment-variables/#add-environment-variables-via-the-dashboard). The build time environment variables (`NEXT_PUBLIC_`) should be set in the [Builds Configuration](https://developers.cloudflare.com/workers/ci-cd/builds/configuration/) so that their value can be inlined by the Workers Builds.
0 commit comments