Skip to content

Commit ea4235a

Browse files
docs: clarify close hook availability across preset types
Note that the close hook only fires on long-running server presets (node, bun, deno) and not in serverless/edge environments. Guide plugin authors toward the response hook or waitUntil for per-request cleanup that works across all presets. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 853f67f commit ea4235a

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

docs/1.docs/50.plugins.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ export default definePlugin((nitro) => {
5454

5555
### Available hooks
5656

57-
- `"request", (event) => {}` - Called when a request is received.
58-
- `"error", (error, { event? }) => {}` - Called when an error is captured.
59-
- `"response", (response, event) => {}` - Called when a response is sent.
60-
- `"close", () => {}` - Called when the server receives a shutdown signal (`SIGTERM` or `SIGINT`).
57+
- `"request", (event) => {}` - Called when a request is received. Available in all presets.
58+
- `"error", (error, { event? }) => {}` - Called when an error is captured. Available in all presets.
59+
- `"response", (response, event) => {}` - Called when a response is sent. Available in all presets.
60+
- `"close", () => {}` - Called when the server receives a shutdown signal (`SIGTERM` or `SIGINT`). Only available in long-running server presets (Node.js, Bun, Deno). Not called in serverless or edge environments.
6161

6262
## Examples
6363

@@ -77,7 +77,7 @@ export default definePlugin((nitro) => {
7777

7878
### Graceful shutdown
7979

80-
When the server receives a shutdown signal (`SIGTERM` or `SIGINT`), the `close` hook is called, allowing plugins to run async cleanup before the process exits. This is useful for flushing telemetry, draining database connections, stopping job queues, and other teardown tasks.
80+
On long-running server presets (`node-server`, `node-cluster`, `bun`, `deno-server`), the `close` hook fires when the process receives `SIGTERM` or `SIGINT`, allowing plugins to run async cleanup before exit.
8181

8282
```ts
8383
import { definePlugin } from "nitro";
@@ -90,6 +90,20 @@ export default definePlugin((nitro) => {
9090
})
9191
```
9292

93+
Serverless and edge runtimes (Cloudflare Workers, AWS Lambda, Vercel, Netlify, Deno Deploy) do not have a shutdown signal–the platform terminates the execution context without notice. The `close` hook will not fire in these environments.
94+
95+
For per-request cleanup that works across all presets, use the `"response"` hook or `request.waitUntil()` instead:
96+
97+
```ts
98+
import { definePlugin } from "nitro";
99+
100+
export default definePlugin((nitro) => {
101+
nitro.hooks.hook("response", async (response, event) => {
102+
await flushRequestTelemetry(event);
103+
});
104+
})
105+
```
106+
93107
### Request and response lifecycle
94108

95109
You can use plugins to register a hook that can run on request lifecycle:

0 commit comments

Comments
 (0)