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
The [Fetch Standard](https://fetch.spec.whatwg.org) allows users to skip consuming the response body by relying on
443
-
[garbage collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#garbage_collection) to release connection resources. Undici does not do the same. Therefore, it is important to always either consume or cancel the response body.
443
+
[garbage collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#garbage_collection) to release connection resources.
444
444
445
445
Garbage collection in Node is less aggressive and deterministic
446
446
(due to the lack of clear idle periods that browsers have through the rendering refresh rate)
447
447
which means that leaving the release of connection resources to the garbage collector can lead
448
448
to excessive connection usage, reduced performance (due to less connection re-use), and even
449
449
stalls or deadlocks when running out of connections.
450
+
Therefore, __it is important to always either consume or cancel the response body anyway__.
450
451
451
452
```js
452
453
// Do
@@ -459,7 +460,15 @@ for await (const chunk of body) {
459
460
const { headers } =awaitfetch(url);
460
461
```
461
462
462
-
The same applies for `request` too:
463
+
However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details.
464
+
465
+
```js
466
+
constheaders=awaitfetch(url, { method:'HEAD' })
467
+
.then(res=>res.headers)
468
+
```
469
+
470
+
Note that consuming the response body is _mandatory_ for `request`:
471
+
463
472
```js
464
473
// Do
465
474
const { body, headers } =awaitrequest(url);
@@ -469,13 +478,6 @@ await res.body.dump(); // force consumption of body
469
478
const { headers } =awaitrequest(url);
470
479
```
471
480
472
-
However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details.
0 commit comments