Skip to content

Commit fc52dbf

Browse files
committed
Document Request.signal
1 parent cf2f61a commit fc52dbf

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Handle request cancellation with Request.signal
3+
description: Workers can now add event listeners on Request.signal and perform tasks when the request is cancelled
4+
products:
5+
- workers
6+
date: 2025-03-14T01:00:00Z
7+
---
8+
9+
import { Render, TypeScriptExample } from "~/components";
10+
11+
`Request` objects now have a `signal` property. You can now attach an event listener to the `signal` and perform tasks when the request to your Worker is canceled by the client.
12+
13+
This lets you do things like clean up after the request or write to logs before your Worker is aborted.
14+
15+
<TypeScriptExample filename="index.ts">
16+
```ts
17+
import { WorkerEntrypoint } from 'cloudflare:workers';
18+
19+
class Example extends WorkerEntrypoint {
20+
async fetch(req: Request): Response {
21+
req.signal.onabort = () => {
22+
console.log('The request was aborted!');
23+
};
24+
25+
const { readable, writable } = new IdentityTransformStream();
26+
this.ctx.waitUntil(this.sendPing(writable));
27+
return new Response(readable);
28+
}
29+
30+
async sendPing(writable: WritableStream): void {
31+
const writer = writable.getWriter();
32+
const enc = new TextEncoder();
33+
34+
for (;;) {
35+
// Send 'ping' every second to keep the connection alive
36+
await writer.write(enc.encode('ping'));
37+
scheduler.wait(1000);
38+
}
39+
}
40+
}
41+
</TypeScriptExample>
42+
43+
For more information see the [`Request` documentation](/workers/runtime-apis/request).
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
_build:
3+
publishResources: false
4+
render: never
5+
list: never
6+
7+
name: "Request.signal is passed through to subrequests"
8+
sort_date: "2025-03-14"
9+
enable_date: "request_signal_passthrough"
10+
disable_date: "no_request_signal_passthrough"
11+
---
12+
13+
When this flag is enabled, the `signal` property of the incoming request to a Worker follows Web-standard behaviour, and will be passed through when the request is cloned, or a new request is created from it. As a result, if the incoming request is canceled by the client, all outgoing subrequests will also be canceled, unless the `signal` property is explicitly modified or cleared.
14+
15+
Because support for Request.signal was recently added to Cloudflare Workers, this behaviour will not apply to existing Workers, and can also be disabled by setting `no_request_signal_passthrough`.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,9 @@ 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. By attaching an event listener to the signal, a Worker can take action when the request made to it is canceled by the client. For example, the Worker may write log entries or perform cleanup tasks before being aborted.
201+
197202
* `url` string read-only
198203

199204
* Contains the URL of the request.

0 commit comments

Comments
 (0)