ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

ref(core): Remove redundant iframe check in supportsNativeFetch - #19853

Closed
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch
Closed

ref(core): Remove redundant iframe check in supportsNativeFetch#19853
HazAT wants to merge 5 commits into
developfrom
bundle-size/supports-native-fetch

Conversation

@HazAT

Copy link
Copy Markdown
Member

Summary

Remove ~30 lines of iframe-based DOM manipulation from supportsNativeFetch(). Saves ~200 bytes gzipped — the single biggest win in this bundle size effort.

Problem

supportsNativeFetch() created a sandboxed iframe to check if fetch is natively implemented. This is identical logic to getNativeImplementation("fetch") in browser-utils, creating two separate iframe-based checks in the CDN bundle.

The function is only called behind a skipNativeFetchCheck guard:

if(skipNativeFetchCheck&&!supportsNativeFetch()){return;}

In the base CDN bundle, skipNativeFetchCheck is never set to true, making the iframe code dead weight. Terser cannot tree-shake it because it cannot prove the parameter is always falsy.

Solution

Simplify supportsNativeFetch() to just delegate to _isFetchSupported() (checks if window.fetch exists). The native-vs-polyfill distinction is handled by getNativeImplementation at call sites that need it.

Also simplifies isNativeFunction from /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/ to /\[native code\]/ — more permissive across different JS engine toString() formats.

Part of #19833.

Co-Authored-By: Claude claude@anthropic.com

Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts
@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.64 kB+0.02%+3 B 🔺
@sentry/browser - with treeshaking flags24.16 kB+0.08%+19 B 🔺
@sentry/browser (incl. Tracing)42.67 kB+0.12%+50 B 🔺
@sentry/browser (incl. Tracing, Profiling)47.32 kB+0.09%+41 B 🔺
@sentry/browser (incl. Tracing, Replay)81.43 kB+0.01%+7 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags71.07 kB+0.11%+75 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas)86.11 kB-0.02%-10 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.38 kB+0.01%+2 B 🔺
@sentry/browser (incl. Feedback)42.45 kB+0.01%+3 B 🔺
@sentry/browser (incl. sendFeedback)30.31 kB+0.01%+2 B 🔺
@sentry/browser (incl. FeedbackAsync)35.37 kB+0.02%+4 B 🔺
@sentry/browser (incl. Metrics)26.93 kB+0.03%+6 B 🔺
@sentry/browser (incl. Logs)27.07 kB+0.01%+2 B 🔺
@sentry/browser (incl. Metrics & Logs)27.74 kB-0.01%-1 B 🔽
@sentry/react27.41 kB+0.06%+16 B 🔺
@sentry/react (incl. Tracing)44.97 kB+0.04%+16 B 🔺
@sentry/vue30.07 kB-0.03%-9 B 🔽
@sentry/vue (incl. Tracing)44.49 kB+0.03%+11 B 🔺
@sentry/svelte25.66 kB-0.02%-4 B 🔽
CDN Bundle28.35 kB+0.25%+70 B 🔺
CDN Bundle (incl. Tracing)43.55 kB+0.1%+42 B 🔺
CDN Bundle (incl. Logs, Metrics)29.2 kB+0.2%+58 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics)44.42 kB+0.16%+67 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics)68.28 kB+0.1%+67 B 🔺
CDN Bundle (incl. Tracing, Replay)80.44 kB+0.14%+111 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.35 kB+0.14%+113 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback)85.96 kB+0.11%+93 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.85 kB+0.09%+78 B 🔺
CDN Bundle - uncompressed82.56 kB-0.08%-58 B 🔽
CDN Bundle (incl. Tracing) - uncompressed128.49 kB-0.06%-75 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed85.43 kB-0.07%-58 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed131.35 kB-0.06%-75 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed209.08 kB-0.02%-37 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed245.36 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed248.22 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed258.27 kB-0.02%-49 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed261.12 kB-0.02%-49 B 🔽
@sentry/nextjs (client)47.4 kB+0.07%+33 B 🔺
@sentry/sveltekit (client)43.08 kB+0.03%+9 B 🔺
@sentry/node-core56.42 kB+0.13%+73 B 🔺
@sentry/node173.38 kB+0.13%+221 B 🔺
@sentry/node - without tracing96.43 kB+0.1%+87 B 🔺
@sentry/aws-serverless113.44 kB+0.09%+100 B 🔺

View base workflow run

@github-actions

github-actionsBot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

ScenarioRequests/s% of BaselinePrev. Requests/sChange %
GET Baseline8,646-8,555+1%
GET With Sentry1,52318%1,560-2%
GET With Sentry (error only)5,73666%5,893-3%
POST Baseline1,136-1,145-1%
POST With Sentry53547%553-3%
POST With Sentry (error only)99688%1,031-3%
MYSQL Baseline3,106-3,189-3%
MYSQL With Sentry32711%401-18%
MYSQL With Sentry (error only)2,55882%2,597-2%

View base workflow run

@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from e2fd3a8 to 39fcc12CompareMarch 19, 2026 11:42
@Lms24
Lms24 requested review from a team as code ownersMarch 19, 2026 11:42
@Lms24Lms24 changed the title refactor(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 changed the title ref(core): Remove redundant iframe check in supportsNativeFetchref(core): Remove redundant iframe check in supportsNativeFetchMar 19, 2026
@Lms24Lms24 self-assigned this Mar 19, 2026
@github-actions

github-actionsBot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Deps

  • Bump mongodb-memory-server-global from 10.1.4 to 11.0.1 by dependabot in #19888
  • Bump stacktrace-parser from 0.1.10 to 0.1.11 by dependabot in #19887

Bug Fixes 🐛

Core

  • Do not overwrite user provided conversation id in Vercel by nicohrubec in #19903
  • Return same value from startSpan as callback returns by s1gr1d in #19300

Other

  • (cloudflare) Forward ctx argument to Workflow.do user callback by Lms24 in #19891
  • (deps) Bump socket.io-parser to 4.2.6 to fix CVE-2026-33151 by chargome in #19880
  • (nestjs) Add node to nest metadata by chargome in #19875
  • (serverless) Add node to metadata by nicohrubec in #19878

Internal Changes 🔧

  • (astro) Re-enable server island tracing e2e test in Astro 6 by Lms24 in #19872
  • (ci) Fix "Gatbsy" typo in issue package label workflow by chargome in #19905
  • (core) Remove redundant iframe check in supportsNativeFetch by HazAT in #19853
  • (lint) Resolve oxlint warnings by isaacs in #19893
  • (node-integration-tests) Remove unnecessary file-type dependency by Lms24 in #19824
  • (sveltekit) Replace recast + @babel/parser with acorn by roli-lpci in #19533
  • Add external contributor to CHANGELOG.md by javascript-sdk-gitflow in #19909

🤖 This preview updates automatically when you update the PR.

@Lms24
Lms24 changed the base branch from autoresearch/browser-bundle-size-2026-03-17 to developMarch 19, 2026 11:49
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 39fcc12 to dff6335CompareMarch 19, 2026 12:13
Comment threadpackages/core/src/utils/supports.ts Outdated
Comment threadpackages/core/src/utils/supports.ts Outdated
// eslint-disable-next-line @typescript-eslint/ban-types
export function isNativeFunction(func: Function): boolean {
return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
return func && /\[native code\]/.test(func.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this. It saves a few bytes but drastically reduces specificity. We have no unit tests that cover this function :(

Comment on lines -111 to -141
if (!_isFetchSupported()) {
return false;
}

// Fast path to avoid DOM I/O
// eslint-disable-next-line @typescript-eslint/unbound-method
if (isNativeFunction(WINDOW.fetch)) {
return true;
}

// window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension)
// so create a "pure" iframe to see if that has native fetch
let result = false;
const doc = WINDOW.document;
// eslint-disable-next-line deprecation/deprecation
if (doc && typeof (doc.createElement as unknown) === 'function') {
try {
const sandbox = doc.createElement('iframe');
sandbox.hidden = true;
doc.head.appendChild(sandbox);
if (sandbox.contentWindow?.fetch) {
// eslint-disable-next-line @typescript-eslint/unbound-method
result = isNativeFunction(sandbox.contentWindow.fetch);
}
doc.head.removeChild(sandbox);
} catch (err) {
DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err);
}
}

return result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is a behaviour change to the original function. Supporting afetch implementation is not the same as supporting/having the native one. I'm going to experiment with moving getNativeImplementation to core so that we can actually reuse the iframe lookup but I'm not sure how much bundle size this still saves.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving old size check for comparison:

size-limit report 📦

⚠️Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

PathSize% ChangeChange
@sentry/browser25.4 kB-0.92%-234 B 🔽
@sentry/browser - with treeshaking flags23.94 kB-0.84%-201 B 🔽
@sentry/browser (incl. Tracing)42.4 kB-0.52%-220 B 🔽
@sentry/browser (incl. Tracing, Profiling)47.06 kB-0.48%-223 B 🔽
@sentry/browser (incl. Tracing, Replay)81.18 kB-0.31%-245 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags70.78 kB-0.3%-211 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas)85.88 kB-0.29%-245 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback)98.14 kB-0.24%-235 B 🔽
@sentry/browser (incl. Feedback)42.22 kB-0.53%-221 B 🔽
@sentry/browser (incl. sendFeedback)30.07 kB-0.78%-234 B 🔽
@sentry/browser (incl. FeedbackAsync)35.13 kB-0.66%-232 B 🔽
@sentry/browser (incl. Metrics)26.68 kB-0.92%-246 B 🔽
@sentry/browser (incl. Logs)26.82 kB-0.91%-245 B 🔽
@sentry/browser (incl. Metrics & Logs)27.51 kB-0.83%-228 B 🔽
@sentry/react27.19 kB-0.75%-203 B 🔽
@sentry/react (incl. Tracing)44.73 kB-0.5%-224 B 🔽
@sentry/vue29.86 kB-0.73%-218 B 🔽
@sentry/vue (incl. Tracing)44.26 kB-0.51%-226 B 🔽
@sentry/svelte25.43 kB-0.93%-238 B 🔽
CDN Bundle28.11 kB-0.61%-172 B 🔽
CDN Bundle (incl. Tracing)43.29 kB-0.49%-213 B 🔽
CDN Bundle (incl. Logs, Metrics)28.97 kB-0.62%-178 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics)44.16 kB-0.44%-195 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics)68.03 kB-0.27%-184 B 🔽
CDN Bundle (incl. Tracing, Replay)80.15 kB-0.23%-179 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)81.04 kB-0.24%-193 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback)85.66 kB-0.25%-211 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)86.55 kB-0.25%-215 B 🔽
CDN Bundle - uncompressed81.97 kB-0.8%-655 B 🔽
CDN Bundle (incl. Tracing) - uncompressed127.89 kB-0.52%-666 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed84.83 kB-0.77%-655 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed130.76 kB-0.51%-666 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed208.47 kB-0.32%-655 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed244.75 kB-0.28%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed247.6 kB-0.27%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed257.66 kB-0.26%-666 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed260.5 kB-0.26%-666 B 🔽
@sentry/nextjs (client)47.16 kB-0.44%-204 B 🔽
@sentry/sveltekit (client)42.82 kB-0.6%-255 B 🔽
@sentry/node-core56.28 kB-0.12%-63 B 🔽
@sentry/node173.21 kB+0.03%+51 B 🔺
@sentry/node - without tracing96.27 kB-0.08%-76 B 🔽
@sentry/aws-serverless113.28 kB-0.06%-62 B 🔽

View base workflow run

Comment threadpackages/core/src/utils/supports.ts
@Lms24

Copy link
Copy Markdown
Member

trying one more approach here to only extract the iframe stuff

Comment threadpackages/browser-utils/src/getNativeImplementation.ts Outdated

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment threadpackages/browser-utils/src/getNativeImplementation.ts
Comment on lines +48 to +61
// This is a really weird fallback but here's what's going on:
// We're just being extra careful here. According to types, windowImpl is _always_ defined.
// However, in some very rare cases (for example test environments), it may in fact not be defined.
// In exactly this case, if we fail to get an iframeImpl, AND no windowImpl either,
// we skip caching and just return effectively undefined (despite types saying it's always defined)
// This basically tricks TS into thinking this function never returns `undefined` which
// for the most part is true.
if (!windowImpl) {
return windowImpl; // but actually return undefined
}

return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]);
// If _only_ iframeImpl is undefined and windowImpl is defined and not not native, we end up here
// In this case, we deliberately cache the windowImpl.
return cacheAndReturn(windowImpl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing this down in case we ever wonder why the hell this code path exists:

This PR DID NOT change any prior behavior. I think this function was already flawed but for the scope of this PR, I'll not fix it right away. Two sketchy things:

  • We have a code path where we effectively return undefined, despite TS assuming we always return a defined implementation (see 1st comment why)
  • We return and cache a non-native, potentially patched version of an implementation in a function that's called getNativeImplementation.

This PR just more clearly call out this behaviour as it was quite hidden away in the prior implementation. I'll investigate if we can fix at least the latter flaw by returning undefined.

HazATand others added 5 commits March 20, 2026 13:24
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
@Lms24
Lms24force-pushed the bundle-size/supports-native-fetch branch from 416056a to f7c95acCompareMarch 20, 2026 12:24
@Lms24

Copy link
Copy Markdown
Member

lol, so after making far to many changes to still get this in without behaviour changes but less duplication, I ended up with a larger bundle size than before. Code is arguably cleaner now but honestly not worth pursuing further. Closing this experiment 😅

@Lms24Lms24 closed this Mar 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@HazAT@Lms24