Skip to content

Commit

Permalink
Always use node-fetch and stop polyfilling
Browse files Browse the repository at this point in the history
Due to a bug in undici (nodejs/undici#1776)
that is affecting many versions of Node and Next.js,
we want a workaround to ensure our users don't see this.

Therefore, we're forking isomorphic-unfetch to always use node-fetch
on Node to avoid falling back to global.fetch (undici).

Also, isomorphic-unfetch was polyfilling, which we want to avoid.
  • Loading branch information
jaslong committed Jan 19, 2023
1 parent 55901ec commit b7ab5c3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
6 changes: 2 additions & 4 deletions packages/isomorphic-unfetch/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
function r(m) {
return (m && m.default) || m;
}
module.exports = global.fetch =
global.fetch ||
(typeof process == "undefined"
? r(require("unfetch"))
module.exports = (typeof process == "undefined"
? global.fetch || r(require("unfetch"))
: function (url, opts) {
if (typeof url === "string" || url instanceof URL) {
url = String(url).replace(/^\/\//g, "https://");
Expand Down
6 changes: 2 additions & 4 deletions packages/isomorphic-unfetch/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
function r(m) {
return (m && m.default) || m;
}
export default global.fetch =
global.fetch ||
(typeof process == "undefined"
? function (url, opts) {
export default fetch = (typeof process == "undefined"
? global.fetch || function (url, opts) {
return import("unfetch").then((m) => r(m)(url, opts));
}
: function (url, opts) {
Expand Down
4 changes: 2 additions & 2 deletions test/isomorphic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("isomorphic-unfetch", () => {
});

describe('"main" entry in NodeJS', () => {
it("should resolve to fetch when window.fetch exists", async () => {
it("should resolve to node-fetch (changed from upstream for Plasmic) when window.fetch exists", async () => {
let sandbox = {
process: {},
global: { fetch },
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("isomorphic-unfetch", () => {

const ns = mod.namespace;

expect(await ns.default("/")).toBe("this is fetch");
expect(await ns.default("/")).toBe("this is node-fetch");
});

it("should resolve to node-fetch when window.fetch does not exist", async () => {
Expand Down

0 comments on commit b7ab5c3

Please sign in to comment.