Skip to content

Commit 979a8f2

Browse files
committed
Small updates to env docs
1 parent 4a8713e commit 979a8f2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/content/changelog/workers/2025-02-28-importable-env.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: Import Env for direct access to bindings
3-
description: More easily configure your worker and call bindings from anywhere in your Worker with an importable `env`
2+
title: Import Env for direct access to bindings in your Worker code
3+
description: More easily configure your Worker and call bindings from anywhere with an importable `env`
44
products:
55
- workers
6-
date: 2025-03-04T11:00:00Z
6+
date: 2025-03-07T19:00:00Z
77
---
88

99
import { Render, TypeScriptExample } from "~/components";
@@ -34,9 +34,14 @@ export default {
3434
```
3535

3636
:::note
37-
Workers error if attempting to perform I/O from outside a request context.
38-
This means that even though `env` is accessible from the top-level scope, not
39-
all bindings will work.
37+
Workers do not allow I/O from outside a request context. This means that even
38+
though `env` is accessible from the top-level scope, you will not be able to access
39+
every binding's methods.
40+
41+
For instance, environment variables and secrets are accessible, and you are able to
42+
call `env.NAMESPACE.get` to get a [Durable Object stub](/durable-objects/api/stub/) in the
43+
top-level context. However, calling methods on the Durable Object stub, making [calls to a KV store](/kv/api/),
44+
and [calling to other Workers](/workers/runtime-apis/bindings/service-bindings) will not work.
4045
:::
4146

4247
Additionally, `env` was normally accessed as a argument to a Worker's entrypoint handler,

src/content/docs/workers/runtime-apis/bindings/index.mdx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ export default {
127127
};
128128
```
129129

130-
Workers do not allow I/O outside of a request context. This means that certain bindings will
131-
not work from the top-level context. For instance, making a call to the [KV binding](/kv/concepts/kv-bindings/)
132-
will error, but making it from within a request will work.
130+
Workers do not allow I/O from outside a request context. This means that even
131+
though `env` is accessible from the top-level scope, you will not be able to access
132+
every binding's methods.
133+
134+
For instance, environment variables and secrets are accessible, and you are able to
135+
call `env.NAMESPACE.get` to get a [Durable Object stub](/durable-objects/api/stub/) in the
136+
top-level context. However, calling methods on the Durable Object stub, making [calls to a KV store](/kv/api/),
137+
and [calling to other Workers](/workers/runtime-apis/bindings/service-bindings) will not work.
133138

134139
```js
135140
import { env } from "cloudflare:workers";
@@ -182,7 +187,7 @@ for dependency injection and testing.
182187
The `withEnv` function provides a mechanism for overriding values of `env`.
183188

184189
Imagine a user has defined the [environment variable](/workers/configuration/environment-variables/)
185-
"NAME" to be "Alice" in their Wrangler configuration file and deployed a worker. By default, logging
190+
"NAME" to be "Alice" in their Wrangler configuration file and deployed a Worker. By default, logging
186191
`env.NAME` would print "Alice". Using the `withEnv` function, you can override the value of
187192
"NAME".
188193

0 commit comments

Comments
 (0)