Isomorphic fetch() with cookie 🍪 and redirect ⏩ support for all environments
npm install real-isomorphic-fetch --save
const IsomorphicFetch = require('real-isomorphic-fetch');
const fetch = require('node-fetch'); // could also be window.fetch in the browser or global.fetch in react-native
const fetchInstance = new IsomorphicFetch(fetch) // cookies are shared between every IsomorphicFetch instance
fetchInstance('https://example.com/123') // Cookies and redirects are handled automatically
.then(response => response.text())
.then(text => console.log(text))
.catch(err => console.error(err));
- There are libraries out there that polyfill fetch in the browser and bring it to node.
- However, node-fetch library doesn't handle receiving cookies...
- ... so you can use fetch-cookie to use cookies with fetch() ...
- ... but then you have to intercept redirects manually because fetch-cookie doesn't handle them...
const fetchWithCookie = require('fetch-cookie')(require('node-fetch'));
fetchWithCookie('https://example.com/123', {
redirect: 'manual'
})
.then(response => response.headers.get('Location'))
.then(location => fetchWithCookie(location, {redirect: 'manual'}))
- ... which then doesn't work in the browser anymore because you can't intercept redirects (and because it uses native dependencies)!
This library handles cookies and redirects together and normalizes the behaviour of node-fetch
to match the one of the browser, so you can use the same syntax in both environments.
This is a rewrite of fetch-cookie. Heavy inspiration also from isomorphic-fetch.
MIT © Jonny Burger