Skip to content

Commit 2654850

Browse files
committed
docs: describe signal property behavior in adapter-node
1 parent 24887c7 commit 2654850

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

documentation/docs/25-build-and-deploy/40-adapter-node.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,49 @@ app.listen(3000, () => {
263263
console.log('listening on port 3000');
264264
});
265265
```
266+
267+
## Aborted requests
268+
269+
> [!NOTE] This feature is available in `vite dev` and `vite preview` servers.
270+
271+
The adapter will fire an `abort` event when the incoming request is cancelled before completion. You can access the [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) via `event.request.signal`.
272+
273+
A pretty typical example of using the `signal` property:
274+
275+
```js
276+
/// file: src/routes/api/object/[slug]/+server.js
277+
/** @type {import('./$types').RequestHandler} */
278+
export async function GET({ request, params }) {
279+
const stream = await s3.getObject("bucket", params.slug, {
280+
signal: request.signal
281+
})
282+
283+
return new Response(stream)
284+
}
285+
```
286+
287+
Another example, this time using [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events):
288+
289+
```js
290+
/// file: src/routes/api/sse/+server.js
291+
/** @type {import('./$types').RequestHandler} */
292+
export async function GET({ request }) {
293+
const stream = new ReadableStream({
294+
start(controller) {
295+
const interval = setInterval(() => {
296+
controller.enqueue("data: Hello, world!\n\n\n");
297+
}, 1000);
298+
299+
request.signal.onabort = () => {
300+
clearInterval(interval);
301+
};
302+
},
303+
});
304+
305+
return new Response(stream, {
306+
headers: {
307+
"Content-Type": "text/event-stream",
308+
},
309+
});
310+
}
311+
```

0 commit comments

Comments
 (0)