Skip to content

Commit

Permalink
fix(nextjs): Use named imports for fetch runtime polyfill
Browse files Browse the repository at this point in the history
Next14 seems to have changed the way it handles default exports when using the webpack bundler for some of their build variants when using `npm run dev`. This commit ensures that we no longer use the default export in an effort to improve compat between the different nextjs versions.

 More information can be found here: https://esbuild.github.io/content-types/#default-interop
 and here: #612
  • Loading branch information
nikosdouvlis committed Oct 26, 2023
1 parent cf36e15 commit a541bcd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/backend/src/runtime/browser/fetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const RuntimeHeaders = Headers;
export const RuntimeRequest = Request;
export const RuntimeResponse = Response;
export const RuntimeAbortController = AbortController;
export const RuntimeFetch = fetch;
15 changes: 10 additions & 5 deletions packages/backend/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
// @ts-ignore - These are package subpaths
import crypto from '#crypto';
// @ts-ignore - These are package subpaths
import fetch from '#fetch';
// @ts-ignore - These are package subpaths
import * as fetchApisPolyfill from '#fetch';

const { RuntimeAbortController, RuntimeBlob, RuntimeFormData, RuntimeHeaders, RuntimeRequest, RuntimeResponse } =
fetchApisPolyfill;
const {
RuntimeFetch,
RuntimeAbortController,
RuntimeBlob,
RuntimeFormData,
RuntimeHeaders,
RuntimeRequest,
RuntimeResponse,
} = fetchApisPolyfill;

type Runtime = {
crypto: Crypto;
Expand All @@ -39,7 +44,7 @@ type Runtime = {
// The globalThis object is supported for Node >= 12.0.
//
// https://github.com/supabase/supabase/issues/4417
const globalFetch = fetch.bind(globalThis);
const globalFetch = RuntimeFetch.bind(globalThis);
// DO NOT CHANGE: Runtime needs to be imported as a default export so that we can stub its dependencies with Sinon.js
// For more information refer to https://sinonjs.org/how-to/stub-dependency/
const runtime: Runtime = {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/runtime/node/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ module.exports.RuntimeHeaders = Headers;
module.exports.RuntimeRequest = Request;
module.exports.RuntimeResponse = Response;
module.exports.RuntimeAbortController = AbortController;
module.exports.RuntimeFetch = fetch;

0 comments on commit a541bcd

Please sign in to comment.