Skip to content

Commit c0c5156

Browse files
committed
Dynamically import the fetch ponyfill in contexts where it isn't available
We're currently always statically importing `cross-fetch` to use as a fetch polyfill. The only environment we actually need to polyfill fetch in is node 16 which is used by old gadget apps on fv1 -- we already only support browsers that have `fetch` defined globally, and node 18 in fv2 has it as well. So, most folks are paying a bundle size penalty for no great reason. Instead, let's move this chunk out into a separate import that is only dynamically imported if needed. This will happen only on node16 which will be nice and snappy, and shouldn't cause a request waterfall in browsers. You'll notice that vite picks up on this dynamic import and creates a new output chunk for it which is what we want for the browser as well. Before: ``` vite v4.4.7 building for production... ✓ 173 modules transformed. dist/api-read-stats.html 210.79 kB │ gzip: 46.88 kB dist/test-bundle-api-read.js 211.80 kB │ gzip: 37.36 kB dist/api-read-stats.html 211.39 kB │ gzip: 46.93 kB dist/test-bundle-api-read.cjs 138.54 kB │ gzip: 30.51 kB ✓ built in 769ms vite v4.4.7 building for production... ✓ 449 modules transformed. dist/react-read-stats.html 302.29 kB │ gzip: 56.83 kB dist/test-bundle-react-read.js 316.93 kB │ gzip: 66.53 kB dist/react-read-stats.html 306.22 kB │ gzip: 56.92 kB dist/test-bundle-react-read.cjs 213.27 kB │ gzip: 55.67 kB ✓ built in 1.13s vite v4.4.7 building for production... ✓ 983 modules transformed. dist/shopify-read-stats.html 515.87 kB │ gzip: 77.42 kB dist/test-bundle-shopify-read.js 636.23 kB │ gzip: 125.20 kB dist/shopify-read-stats.html 516.37 kB │ gzip: 77.42 kB dist/test-bundle-shopify-read.cjs 447.28 kB │ gzip: 106.73 kB ✓ built in 2.29s ``` After: ``` vite v4.4.7 building for production... ✓ 173 modules transformed. dist/api-read-stats.html 210.92 kB │ gzip: 46.91 kB dist/browser-ponyfill-a950d05c.js 12.53 kB │ gzip: 3.50 kB dist/test-bundle-api-read.js 199.78 kB │ gzip: 34.45 kB dist/api-read-stats.html 211.51 kB │ gzip: 46.98 kB dist/browser-ponyfill-0b1c63f2.cjs 8.88 kB │ gzip: 3.07 kB dist/test-bundle-api-read.cjs 130.06 kB │ gzip: 28.07 kB ✓ built in 774ms vite v4.4.7 building for production... ✓ 449 modules transformed. dist/react-read-stats.html 302.31 kB │ gzip: 56.84 kB dist/test-bundle-react-read.js 0.09 kB │ gzip: 0.10 kB dist/browser-ponyfill-aae43e35.js 12.44 kB │ gzip: 3.50 kB dist/react-read-b8f94e10.js 305.18 kB │ gzip: 63.69 kB dist/react-read-stats.html 306.31 kB │ gzip: 56.96 kB dist/test-bundle-react-read.cjs 0.17 kB │ gzip: 0.16 kB dist/browser-ponyfill-1992ee4c.cjs 8.83 kB │ gzip: 3.08 kB dist/react-read-e3b5cb04.cjs 204.97 kB │ gzip: 53.20 kB ✓ built in 1.16s vite v4.4.7 building for production... ✓ 983 modules transformed. dist/shopify-read-stats.html 514.75 kB │ gzip: 77.59 kB dist/test-bundle-shopify-read.js 0.12 kB │ gzip: 0.12 kB dist/browser-ponyfill-d3f0b39a.js 12.45 kB │ gzip: 3.50 kB dist/shopify-read-62230a97.js 624.51 kB │ gzip: 122.38 kB dist/shopify-read-stats.html 515.26 kB │ gzip: 77.60 kB dist/test-bundle-shopify-read.cjs 0.19 kB │ gzip: 0.17 kB dist/browser-ponyfill-8417ec9c.cjs 8.83 kB │ gzip: 3.08 kB dist/shopify-read-25c98907.cjs 439.00 kB │ gzip: 104.22 kB ``` Another 6% bundle size reduction
1 parent b18375e commit c0c5156

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/api-client-core/src/GadgetConnection.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ClientOptions, RequestPolicy } from "@urql/core";
22
import { Client, cacheExchange, fetchExchange, subscriptionExchange } from "@urql/core";
3-
import fetchPolyfill from "cross-fetch";
3+
44
import type { ExecutionResult } from "graphql";
55
import type { Sink, Client as SubscriptionClient, ClientOptions as SubscriptionClientOptions } from "graphql-ws";
66
import { CloseCode, createClient as createSubscriptionClient } from "graphql-ws";
@@ -39,7 +39,7 @@ export interface GadgetConnectionOptions {
3939
websocketsEndpoint?: string;
4040
subscriptionClientOptions?: GadgetSubscriptionClientOptions;
4141
websocketImplementation?: any;
42-
fetchImplementation?: typeof fetchPolyfill;
42+
fetchImplementation?: typeof globalThis.fetch;
4343
environment?: "Development" | "Production";
4444
requestPolicy?: ClientOptions["requestPolicy"];
4545
applicationId?: string;
@@ -71,7 +71,7 @@ export class GadgetConnection {
7171
private subscriptionClientOptions?: SubscriptionClientOptions;
7272
private websocketsEndpoint: string;
7373
private websocketImplementation: any;
74-
private _fetchImplementation: typeof fetchPolyfill;
74+
private _fetchImplementation: typeof globalThis.fetch;
7575
private environment: "Development" | "Production";
7676
private exchanges: Required<Exchanges>;
7777

@@ -95,7 +95,10 @@ export class GadgetConnection {
9595
} else if (typeof window != "undefined" && window.fetch) {
9696
this._fetchImplementation = window.fetch.bind(window);
9797
} else {
98-
this._fetchImplementation = fetchPolyfill;
98+
this._fetchImplementation = async (...args: [any]) => {
99+
const { fetch } = await import("cross-fetch");
100+
return await fetch(...args);
101+
};
99102
}
100103
this.websocketImplementation = options.websocketImplementation ?? globalThis?.WebSocket ?? WebSocket;
101104
this.websocketsEndpoint = options.websocketsEndpoint ?? options.endpoint + "/batch";
@@ -124,7 +127,7 @@ export class GadgetConnection {
124127
return this.currentTransaction?.client || this.baseClient;
125128
}
126129

127-
set fetchImplementation(implementation: typeof fetchPolyfill) {
130+
set fetchImplementation(implementation: typeof globalThis.fetch) {
128131
this._fetchImplementation = implementation;
129132
this.resetClients();
130133
}

0 commit comments

Comments
 (0)