Skip to content

Commit 39fcc12

Browse files
HazATLms24
authored andcommitted
refactor(core): Remove redundant iframe check in supportsNativeFetch
supportsNativeFetch() created a sandboxed iframe to check if the Fetch API is natively implemented — identical logic to getNativeImplementation('fetch') in browser-utils. The iframe approach adds ~30 lines of DOM manipulation code. The function is only called behind a `skipNativeFetchCheck` guard that is never set to true in the base CDN bundle, making the iframe code dead weight that cannot be tree-shaken by terser (it can't prove the parameter is always falsy across the program). Simplify to delegate to `_isFetchSupported()` which checks if `window.fetch` exists. The actual native-vs-polyfill distinction is already handled by `getNativeImplementation` at the call sites that need it. Also simplifies the `isNativeFunction` regex from an exact whitespace pattern to a simpler `/[native code]/` check, which is more permissive across different JS engine toString() formats. Saves ~200 bytes gzipped on the base browser bundle. Co-Authored-By: Claude claude@anthropic.com
1 parent 938ab2d commit 39fcc12

File tree

1 file changed

+2
-32
lines changed

1 file changed

+2
-32
lines changed

packages/core/src/utils/supports.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function _isFetchSupported(): boolean {
9494
*/
9595
// eslint-disable-next-line @typescript-eslint/ban-types
9696
export function isNativeFunction(func: Function): boolean {
97-
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
97+
return func && /\[native code\]/.test(func.toString());
9898
}
9999

100100
/**
@@ -108,37 +108,7 @@ export function supportsNativeFetch(): boolean {
108108
return true;
109109
}
110110

111-
if (!_isFetchSupported()) {
112-
return false;
113-
}
114-
115-
// Fast path to avoid DOM I/O
116-
// eslint-disable-next-line @typescript-eslint/unbound-method
117-
if (isNativeFunction(WINDOW.fetch)) {
118-
return true;
119-
}
120-
121-
// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
122-
// so create a "pure" iframe to see if that has native fetch
123-
let result = false;
124-
const doc = WINDOW.document;
125-
// eslint-disable-next-line deprecation/deprecation
126-
if (doc && typeof (doc.createElement as unknown) === 'function') {
127-
try {
128-
const sandbox = doc.createElement('iframe');
129-
sandbox.hidden = true;
130-
doc.head.appendChild(sandbox);
131-
if (sandbox.contentWindow?.fetch) {
132-
// eslint-disable-next-line @typescript-eslint/unbound-method
133-
result = isNativeFunction(sandbox.contentWindow.fetch);
134-
}
135-
doc.head.removeChild(sandbox);
136-
} catch (err) {
137-
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
138-
}
139-
}
140-
141-
return result;
111+
return _isFetchSupported();
142112
}
143113

144114
/**

0 commit comments

Comments
 (0)