pub struct Context { /* private fields */ }Expand description
A context bound to a fetch event.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new(inner: JsContext) -> Self
pub fn new(inner: JsContext) -> Self
Constructs a context from an underlying JavaScript context object.
Sourcepub fn wait_until<F>(&self, future: F)
pub fn wait_until<F>(&self, future: F)
Extends the lifetime of the “fetch” event which this context is bound to, until the given future has been completed. The future is executed before the handler terminates but does not block the response. For example, this is ideal for caching responses or handling logging.
context.wait_until(async move {
let _ = cache.put(request, response).await;
});Sourcepub fn pass_through_on_exception(&self)
pub fn pass_through_on_exception(&self)
Prevents a runtime error response when the Worker script throws an unhandled exception. Instead, the script will “fail open”, which will proxy the request to the origin server as though the Worker was never invoked.
Sourcepub fn props<T: DeserializeOwned>(&self) -> Result<T>
pub fn props<T: DeserializeOwned>(&self) -> Result<T>
Get the props passed to this worker execution context.
Props provide a way to pass additional configuration to a worker based on the context in which it was invoked. For example, when your Worker is called by another Worker via a Service Binding, props can provide information about the calling worker.
Props are configured in your wrangler.toml when setting up Service Bindings:
[[services]]
binding = "MY_SERVICE"
service = "my-worker"
props = { clientId = "frontend", permissions = ["read", "write"] }Then deserialize them to your custom type:
use serde::Deserialize;
#[derive(Deserialize)]
struct MyProps {
clientId: String,
permissions: Vec<String>,
}
let props = ctx.props::<MyProps>()?;See: https://developers.cloudflare.com/workers/runtime-apis/context/#props