Skip to content

Commit d864684

Browse files
committed
ref: Add fast-path to fetchImpl
1 parent bcac95d commit d864684

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/browser/src/transports/fetch.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { eventToSentryRequest, sessionToSentryRequest } from '@sentry/core';
22
import { Event, Response, SentryRequest, Session, TransportOptions } from '@sentry/types';
3-
import { getGlobalObject, logger, supportsReferrerPolicy, SyncPromise } from '@sentry/utils';
3+
import { getGlobalObject, isNativeFetch, logger, supportsReferrerPolicy, SyncPromise } from '@sentry/utils';
44

55
import { BaseTransport } from './base';
66

@@ -45,8 +45,13 @@ type FetchImpl = typeof fetch;
4545
* Safari: resource blocked by content blocker
4646
*/
4747
function getNativeFetchImplementation(): FetchImpl {
48-
// Make sure that the fetch we use is always the native one.
48+
// Fast path to avoid DOM I/O
4949
const global = getGlobalObject<Window>();
50+
// eslint-disable-next-line @typescript-eslint/unbound-method
51+
if (isNativeFetch(global.fetch)) {
52+
return global.fetch.bind(global);
53+
}
54+
5055
const document = global.document;
5156
// eslint-disable-next-line deprecation/deprecation
5257
if (typeof document?.createElement === `function`) {

packages/utils/src/supports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function supportsFetch(): boolean {
7373
* isNativeFetch checks if the given function is a native implementation of fetch()
7474
*/
7575
// eslint-disable-next-line @typescript-eslint/ban-types
76-
function isNativeFetch(func: Function): boolean {
76+
export function isNativeFetch(func: Function): boolean {
7777
return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString());
7878
}
7979

0 commit comments

Comments
 (0)