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
* Document Request.signal
* Apply improved wording from @irvinebroque
Co-authored-by: Brendan Irvine-Broque <[email protected]>
* Improve example and include it in the documentation of Request
* Update docs to indicate a compat flag is required
* Wording improvements suggested by @mikenomitch
---------
Co-authored-by: Brendan Irvine-Broque <[email protected]>
title: Handle incoming request cancellation in Workers with Request.signal
3
+
description: Workers can now add event listeners on Request.signal and perform tasks when the request is cancelled by the client
4
+
products:
5
+
- workers
6
+
date: 2025-05-22T01:00:00Z
7
+
---
8
+
9
+
import { Render } from"~/components";
10
+
11
+
In Cloudflare Workers, you can now attach an event listener to [`Request`](/workers/runtime-apis/request/) objects, using the [`signal` property](https://developer.mozilla.org/en-US/docs/Web/API/Request/signal). This allows you to perform tasks when the request to your Worker is canceled by the client. To use this feature, you must set the [`enable_request_signal`](/workers/configuration/compatibility-flags/#enable-requestsignal-for-incoming-requests) compatibility flag.
12
+
13
+
You can use a listener to perform cleanup tasks or write to logs before your Worker's invocation ends. For example, if you run the Worker below, and then abort the request from the client, a log will be written:
name: "Enable `Request.signal` for incoming requests"
3
+
sort_date: "2025-05-22"
4
+
enable_flag: "enable_request_signal"
5
+
disable_flag: "disable_request_signal"
6
+
---
7
+
8
+
When you use the `enable_request_signal` compatibility flag, you can attach an event listener to [`Request`](/workers/runtime-apis/request/) objects, using the [`signal` property](https://developer.mozilla.org/en-US/docs/Web/API/Request/signal). This allows you to perform tasks when the request to your Worker is canceled by the client.
The [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request) interface represents an HTTP request and is part of the [Fetch API](/workers/runtime-apis/fetch/).
12
12
@@ -88,6 +88,8 @@ An object containing properties that you want to apply to the request.
88
88
89
89
* The redirect mode to use: `follow`, `error`, or `manual`. The default for a new `Request` object is `follow`. Note, however, that the incoming `Request` property of a `FetchEvent` will have redirect mode `manual`.
90
90
91
+
*`signal` AbortSignal optional
92
+
* If provided, the request can be canceled by triggering an abort on the corresponding `AbortController`.
91
93
92
94
93
95
#### The `cf` property (`RequestInitCfProperties`)
@@ -194,6 +196,11 @@ All properties of an incoming `Request` object (the request you receive from the
194
196
195
197
* The redirect mode to use: `follow`, `error`, or `manual`. The `fetch` method will automatically follow redirects if the redirect mode is set to `follow`. If set to `manual`, the `3xx` redirect response will be returned to the caller as-is. The default for a new `Request` object is `follow`. Note, however, that the incoming `Request` property of a `FetchEvent` will have redirect mode `manual`.
196
198
199
+
*`signal` AbortSignal read-only
200
+
* The `AbortSignal` corresponding to this request. If you use the [`enable_request_signal`](/workers/configuration/compatibility-flags/#enable-requestsignal-for-incoming-requests) compatibility flag, you can attach an event listener to the signal. This allows you to perform cleanup tasks or write to logs before your Worker's invocation ends.
201
+
For example, if you run the Worker below, and then abort the request from the client, a log will be written:
0 commit comments