kyw
kyw

Reputation: 7555

How to integrate whatwg-fetch in Gatsby

Does anyone know how to integrate the whatwg-fetch fetch polyfill in gatsby?

What I have done so far is import 'whatwg-fetch'; in the gatsby-browser.js. Now I'm not sure how to add it as a first element in the webpack's entry property presumably in the gatsby-node.js.

Upvotes: 2

Views: 1292

Answers (1)

jgautsch
jgautsch

Reputation: 301

Yes, here is a copy of my (tested and working) gatsby-browser.js:

import 'whatwg-fetch' // require('whatwg-fetch') // if it's gatsby v2 - https://gatsby.app/no-mixed-modules

exports.onClientEntry = () => {
  // Don't need to do anything here, but if you don't
  // export something, the import won't work.
}

No need to add whatwg-fetch to webpack's entry property in gatsby-node.js.

Also whatwg-fetch depends on Promises, but Promise is already polyfilled in gatsby, so no need to add an extra polyfill for Promise.

Upvotes: 6

Related Questions