Reputation: 2631
I need to know in my worker script if it is running using wrangler dev
locally
or it is running at cloudflare after wrangelr publish
.
Is there an environment variable that tells me that, or a request headers?
Code snippet would be highly appreciated.
Upvotes: 2
Views: 3092
Reputation: 17481
There isn't a builtin variable, but you can populate such info yourself by defining environments in your wrangler.toml
For example, if we say the topmost [vars]
are meant to be used in production, we can declare another variable set meant to be used in local
environment. (the environment name is irrelevant)
type = "webpack"
webpack_config = "webpack.config.js"
# these will be used in production
vars = { WORKER_ENV = "production", SENTRY_ENABLED = true }
[env.local]
# these will be used only when --env=local
vars = { WORKER_ENV = "local", SENTRY_ENABLED = false }
From then on, if you run your worker locally using
wrangler dev --env=local
the value of binding WORKER_ENV
will be populated as defined under [env.local.vars]
.
By the way, the syntax of wrangler.toml
above is equivalent to
type = "webpack"
webpack_config = "webpack.config.js"
[vars]
WORKER_ENV = "production"
SENTRY_ENABLED = true
[env]
[env.local]
[env.local.vars]
WORKER_ENV = "local"
SENTRY_ENABLED = false
Which I believe it's easier to understand
Upvotes: 4
Reputation: 3414
What also could work is to use different build
commands locally and for releases.
For a CompiledWasm
script, I set the following in wrangler.toml
:
[build]
command = "cargo install -q worker-build && worker-build --debug"
Now in local development, the following function returns false:
pub fn is_release() -> bool {
cfg!(not(debug_assertions))
}
Next, in the CI release configuration, I've added a Bash script to overwrite the --debug
flag:
- run: |
command = "cargo install -q worker-build && worker-build"
sed -i 's/worker-build --debug/worker-build --release/' wrangler.toml
if grep -q "worker-build --release" wrangler.toml; then
echo "Replacement succeeded"
else
echo "Replacement failed"
exit 1
fi
This avoids having to pass the RouteContext
around in order to check whether the program is in release or development mode.
Upvotes: 0
Reputation: 1200
This is the way that I am using:
Create a file .dev.var
in the root of project and define a variable to detect the environment. In below ex I am using IS_LOCAL_MODE
IS_LOCAL_MODE=1
Now put this variable to Cloudflare to use it in the production environment
wrangler secret put IS_LOCAL_MODE
Then fill the value is 0
That's it. Now in your code, to check the mode we will do something like this
if(env.IS_LOCAL_MODE == 1){
console.log("Hello admin, you are in dev mode")
}else{
console.log("Silence is gold!")
}
Upvotes: 0
Reputation: 131
I believe there is an official update for this now (as of November 2022):
When developing locally, you can create a .dev.vars file in the project root which allows you to define variables that will be used when running wrangler dev or wrangler pages dev, as opposed to using another environment and [vars] in wrangler.toml.
Cloudflare Workers documentation
Upvotes: 2