-
Notifications
You must be signed in to change notification settings - Fork 10k
Document Request.signal #20843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mikenomitch
merged 5 commits into
cloudflare:production
from
npaun:npaun/docs-request-signal
May 27, 2025
+69
−1
Merged
Document Request.signal #20843
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f7ae260
Document Request.signal
npaun 08ac85e
Apply improved wording from @irvinebroque
npaun ec9f8ce
Improve example and include it in the documentation of Request
npaun 0e1e297
Update docs to indicate a compat flag is required
npaun 50c33fc
Wording improvements suggested by @mikenomitch
npaun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/content/changelog/workers/2025-05-22-handle-request-cancellation.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| title: Handle incoming request cancellation in Workers with Request.signal | ||
| description: Workers can now add event listeners on Request.signal and perform tasks when the request is cancelled | ||
| products: | ||
| - workers | ||
| date: 2025-05-22T01:00:00Z | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; | ||
|
|
||
| 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. To use this feature, you must set the `enable_request_signal` compatibility flag. | ||
npaun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
npaun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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: | ||
|
|
||
| <Render file="request-signal-example" product="workers" /> | ||
|
|
||
|
|
||
| For more information see the [`Request` documentation](/workers/runtime-apis/request). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| name: "Enable `Request.signal` for incoming requests" | ||
| sort_date: "2025-05-22" | ||
| enable_flag: "enable_request_signal" | ||
| disable_flag: "disable_request_signal" | ||
| --- | ||
|
|
||
| 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), and perform tasks when the request to your Worker is canceled by the client. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| {} | ||
|
|
||
| --- | ||
|
|
||
| import { TypeScriptExample } from "~/components"; | ||
|
|
||
| <TypeScriptExample filename="index.ts"> | ||
| ```ts | ||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| request.signal.addEventListener('abort', () => { | ||
npaun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log('The request was aborted!'); | ||
| }); | ||
|
|
||
| const { readable, writable } = new IdentityTransformStream(); | ||
| sendPing(writable); | ||
| return new Response(readable, { headers: { 'Content-Type': 'text/plain' } }); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
|
|
||
| async function sendPing(writable: WritableStream): Promise<void> { | ||
| const writer = writable.getWriter(); | ||
| const enc = new TextEncoder(); | ||
|
|
||
| for (;;) { | ||
| // Send 'ping' every second to keep the connection alive | ||
| await writer.write(enc.encode('ping\r\n')); | ||
| await scheduler.wait(1000); | ||
| } | ||
| } | ||
| ``` | ||
| </TypeScriptExample> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.