Skip to content

Commit 7e88a68

Browse files
committed
Add docs on polyfilling fetch
1 parent 4e27dd7 commit 7e88a68

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/deploying/custom-node.md

+38
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,41 @@ title: Custom Node.js
77
<docs-warning>
88
This document is a work in progress. There's not much to see here (yet).
99
</docs-warning>
10+
11+
## Polyfilling `fetch`
12+
13+
React Router officially supports Active and Maintenance[^1] [Node LTS veleases][node-releases] at any given point in time. Dropping support for End of Life Node versions may be done in a React Router Minor release.
14+
15+
[^1] Based on timing, React Router may drop support for a Node Maintenance LTS version shortly before it goes end-of-life if it better aligns with a React Router Major SemVer release.
16+
17+
At the time React Router v7 was released, all versions had a usable `fetch` implementation so there is generally no need to polyfill any `fetch` APIs so long as you're on Node 22 or one of the later Node 20 releases.
18+
19+
- Node 22 (Active LTS) has a stable [`fetch`][node-22-fetch] implementation
20+
- Node 20 (Maintenance LTS) has an experimental (but suitable from our testing) [`fetch`][node-20-fetch] implementation
21+
22+
If you do find that you need to polyfill anything, you can do so directly from the [undici] package which node uses internally.
23+
24+
```ts
25+
import {
26+
fetch as nodeFetch,
27+
File as NodeFile,
28+
FormData as NodeFormData,
29+
Headers as NodeHeaders,
30+
Request as NodeRequest,
31+
Response as NodeResponse,
32+
} from "undici";
33+
34+
export function polyfillFetch() {
35+
global.File = NodeFile;
36+
global.Headers = NodeHeaders;
37+
global.Request = NodeRequest;
38+
global.Response = NodeResponse;
39+
global.fetch = nodeFetch;
40+
global.FormData = NodeFormData;
41+
}
42+
```
43+
44+
[node-releases]: https://nodejs.org/en/about/previous-releases
45+
[node-20-fetch]: https://nodejs.org/docs/latest-v20.x/api/globals.html#fetch
46+
[node-22-fetch]: https://nodejs.org/docs/latest-v22.x/api/globals.html#fetch
47+
[undici]: https://github.com/nodejs/undici

0 commit comments

Comments
 (0)