Skip to content

Commit 3ca5da9

Browse files
npaunirvinebroque
andauthored
Document Request.signal (#20843)
* 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]>
1 parent 2013069 commit 3ca5da9

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
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:
14+
15+
<Render file="request-signal-example" product="workers" />
16+
17+
18+
For more information see the [`Request` documentation](/workers/runtime-apis/request).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
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.

src/content/docs/workers/runtime-apis/request.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Interface that represents an HTTP request.
66

77
---
88

9-
import { Type, MetaInfo } from "~/components";
9+
import { Type, MetaInfo, Render } from "~/components";
1010

1111
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/).
1212

@@ -88,6 +88,8 @@ An object containing properties that you want to apply to the request.
8888

8989
* 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`.
9090

91+
* `signal` AbortSignal optional
92+
* If provided, the request can be canceled by triggering an abort on the corresponding `AbortController`.
9193

9294

9395
#### The `cf` property (`RequestInitCfProperties`)
@@ -194,6 +196,11 @@ All properties of an incoming `Request` object (the request you receive from the
194196

195197
* 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`.
196198

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:
202+
<Render file="request-signal-example" />
203+
197204
* `url` string read-only
198205

199206
* Contains the URL of the request.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
{}
3+
4+
---
5+
6+
import { TypeScriptExample } from "~/components";
7+
8+
<TypeScriptExample filename="index.ts">
9+
```ts
10+
export default {
11+
async fetch(request, env, ctx): Promise<Response> {
12+
// This sets up an event listener that will be called if the client disconnects from your
13+
// worker.
14+
request.signal.addEventListener('abort', () => {
15+
console.log('The request was aborted!');
16+
});
17+
18+
const { readable, writable } = new IdentityTransformStream();
19+
sendPing(writable);
20+
return new Response(readable, { headers: { 'Content-Type': 'text/plain' } });
21+
},
22+
} satisfies ExportedHandler<Env>;
23+
24+
async function sendPing(writable: WritableStream): Promise<void> {
25+
const writer = writable.getWriter();
26+
const enc = new TextEncoder();
27+
28+
for (;;) {
29+
// Send 'ping' every second to keep the connection alive
30+
await writer.write(enc.encode('ping\r\n'));
31+
await scheduler.wait(1000);
32+
}
33+
}
34+
```
35+
</TypeScriptExample>

0 commit comments

Comments
 (0)