Skip to content

Commit ec9f8ce

Browse files
committed
Improve example and include it in the documentation of Request
1 parent 08ac85e commit ec9f8ce

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

src/content/changelog/workers/2025-03-14-handle-request-cancellation.mdx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,12 @@ products:
66
date: 2025-03-14T01:00:00Z
77
---
88

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

1111
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), and perform tasks when the request to your Worker is canceled by the client.
1212

1313
This allows you 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:
1414

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>
15+
<Render file="request-signal-example" product="workers" />
4216

4317
For more information see the [`Request` documentation](/workers/runtime-apis/request).

src/content/compatibility-flags/request-signal-passthrough.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ _build:
66

77
name: "Request.signal is passed through to subrequests"
88
sort_date: "2025-03-14"
9-
enable_date: "request_signal_passthrough"
10-
disable_date: "no_request_signal_passthrough"
9+
enable_date: "2025-04-14"
10+
enable_flag: "request_signal_passthrough"
11+
disable_flag: "no_request_signal_passthrough"
1112
---
1213

1314
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.

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

Lines changed: 4 additions & 2 deletions
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

@@ -197,7 +197,9 @@ All properties of an incoming `Request` object (the request you receive from the
197197
* 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`.
198198

199199
* `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.
200+
* The `AbortSignal` corresponding to this request. By attaching an event listener to the signal, you can 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" />
201203

202204
* `url` string read-only
203205

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
request.signal.onabort = () => {
13+
console.log('The request was aborted!');
14+
};
15+
16+
const { readable, writable } = new IdentityTransformStream();
17+
ctx.waitUntil(sendPing(writable));
18+
return new Response(readable);
19+
},
20+
} satisfies ExportedHandler<Env>;
21+
22+
23+
async function sendPing(writable: WritableStream): Promise<void> {
24+
const writer = writable.getWriter();
25+
const enc = new TextEncoder();
26+
27+
for (;;) {
28+
// Send 'ping' every second to keep the connection alive
29+
await writer.write(enc.encode('ping\r\n'));
30+
await scheduler.wait(1000);
31+
}
32+
}
33+
```
34+
</TypeScriptExample>

0 commit comments

Comments
 (0)