I study patterns used in an open source project found on Github Trending. For this week, I reviewed some parts of Meshery codebase and wrote this article.
On Node, node-fetch is always used instead of global.fetch. This is a workaround this undici bug which is negatively affecting many versions of Node and Next.js.
Don’t polyfill, leave global.fetch alone.
Applies a patch on node-fetch-cjs to this node-fetch bug by following this PR. Then we inline node-fetch-cjs into the built version of server.js, so that it will contain our patched version of node-fetch-cjs.
So their difference is only in the client and the question reduces to: What’s the difference between whatwg-fetch and unfetch?
The whatwg-fetch package is an almost complete polyfill for fetch API, but the unfetch is built with “bundle size” as the first priority in mind (like the other packages written by Jason Miller such as preact). The unfetch only supports a subset of the full fetch API. However, it’s great if you just need simple requests in your application.
A good strategy for new applications is to use unfetch at first, and replace it with whatwg-fetch if you need any feature (such as streaming) which is not supported in unfetch.