Isomorphic Unfetch
Switches between unfetch & node-fetch for client & server.
Fork differences:
- 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.
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm i isomorphic-unfetch
Then with a module bundler like rollup or webpack, use as you would anything else:
import fetch from 'isomorphic-unfetch'
const fetch = require('isomorphic-unfetch')
Usage
As a ponyfill:
import fetch from 'isomorphic-unfetch';
fetch('/foo.json')
.then( r => r.json() )
.then( data => {
console.log(data);
});
Globally, as a polyfill:
import 'isomorphic-unfetch';
fetch('/foo.json')
.then( r => r.json() )
.then( data => {
console.log(data);
});
License
MIT License © Jason Miller