You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8f8f95b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/1.guide/04.middleware.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,12 @@ serve({
37
37
});
38
38
```
39
39
40
+
## Modifying a response
41
+
42
+
A middleware that decorates a response — a `Set-Cookie`, a CORS or cache header — should mutate `res.headers` in place, as `xPoweredBy` does above. Rebuilding the response around its body (`new Response(res.body, { ... })`) reads `res.body`, which on Node.js discards the [`FastResponse`](/guide/node#fastresponse) fast path and costs roughly 2x the throughput on a small response.
43
+
44
+
Response headers are mutable on every runtime srvx supports, so this works regardless of which class the handler returned. The exception is a response from `fetch()`, `Response.error()` or `Response.redirect()`, whose headers the Fetch standard marks immutable — rebuild those instead.
45
+
40
46
## Order of execution
41
47
42
48
Middleware run in the order they appear in the `middleware` array, each wrapping the next, with your `fetch` handler at the center. A middleware that returns a response **without** calling `next()` short-circuits the rest of the chain — nothing after it runs.
Copy file name to clipboardExpand all lines: docs/1.guide/08.node.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,6 +118,32 @@ console.log(`Server running at ${server.url}`);
118
118
119
119
You can locally run benchmarks by cloning [srvx repository](https://github.com/h3js/srvx) and running `npm run bench:node [--all]` script. Speedup in v22.8.0 was roughly **%94**!
120
120
121
+
### Adding headers to a response
122
+
123
+
`FastResponse` keeps the body you passed in exactly as you passed it — a string stays a string all the way to `res.write()`, and its `Content-Type` and `Content-Length` are derived from it at send time. Reading `response.body` gives up that fast path: it forces the full [Response][Response] internals to be built and turns the body into a [ReadableStream][ReadableStream].
124
+
125
+
So to add a header — a `Set-Cookie`, a CORS or cache header, anything a middleware wants to attach to a response it did not create — **mutate `response.headers` in place** rather than rebuilding the response around its body:
Rebuilding costs roughly **2x** the throughput of mutating on a small response, because the raw body is replaced by a stream that has to be pumped chunk by chunk instead of written once with a known length.
141
+
142
+
`response.headers` on a `FastResponse` is mutable, whether or not the body has already been materialized, and mutations are guaranteed to reach the wire. This also holds for a plain `new Response()` on Node.js, Deno and Bun, so a middleware can mutate without knowing which class it was handed. The exceptions are responses whose headers the [Fetch standard](https://fetch.spec.whatwg.org/#concept-headers-guard) marks immutable — those returned by `fetch()`, `Response.error()` and `Response.redirect()`. For those, rebuilding is the only option, and it costs nothing: their bodies are already streams.
143
+
144
+
> [!NOTE]
145
+
> A `FastResponse` may only be sent once. Its body and headers are released as it is written to the socket, so a cached instance replayed across requests sends an empty body the second time. Build a new response per request, or cache the body and construct around it.
146
+
121
147
## Fetching Node.js Handlers
122
148
123
149
srvx server converts a [fetch][fetch]-like [Request][Request] => [Response][Response] handler to [node:IncomingMessage][IncomingMessage] => [node:ServerResponse][ServerResponse] handler that is compatible with Node.js built-in server.
0 commit comments