Skip to content

Commit ae94c1a

Browse files
mydeaisaacs
authored andcommitted
feat(core): Only check for native fetch in browser
1 parent 8f4f24d commit ae94c1a

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

packages/browser/src/integrations/httpclient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function _wrapFetch(client: Client, options: HttpClientOptions): void {
298298
}
299299

300300
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, error || virtualError);
301-
}, false);
301+
});
302302
}
303303

304304
/**

packages/cloudflare/src/integrations/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const _fetchIntegration = ((options: Partial<Options> = {}) => {
107107
if (breadcrumbs) {
108108
createBreadcrumb(handlerData);
109109
}
110-
}, true);
110+
});
111111
},
112112
setup(client) {
113113
HAS_CLIENT_MAP.set(client, true);

packages/core/src/instrument/fetch.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getClient } from '../currentScopes';
33
import type { HandlerDataFetch } from '../types/instrument';
44
import type { WebFetchHeaders } from '../types/webfetchapi';
55
import { isError, isObjectLike, isRequest } from '../utils/is';
6+
import { isBrowser } from '../utils/isBrowser';
67
import { addNonEnumerableProperty, fill } from '../utils/object';
78
import { supportsNativeFetch } from '../utils/supports';
89
import { timestampInSeconds } from '../utils/time';
@@ -20,13 +21,10 @@ type FetchResource = string | { toString(): string } | { url: string };
2021
* Use at your own risk, this might break without changelog notice, only used internally.
2122
* @hidden
2223
*/
23-
export function addFetchInstrumentationHandler(
24-
handler: (data: HandlerDataFetch) => void,
25-
skipNativeFetchCheck?: boolean,
26-
): () => void {
24+
export function addFetchInstrumentationHandler(handler: (data: HandlerDataFetch) => void): () => void {
2725
const type = 'fetch';
2826
const removeHandler = addHandler(type, handler);
29-
maybeInstrument(type, () => instrumentFetch(undefined, skipNativeFetchCheck));
27+
maybeInstrument(type, () => instrumentFetch());
3028
return removeHandler;
3129
}
3230

@@ -46,8 +44,12 @@ export function addFetchEndInstrumentationHandler(handler: (data: HandlerDataFet
4644
return removeHandler;
4745
}
4846

49-
function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNativeFetchCheck: boolean = false): void {
50-
if (skipNativeFetchCheck && !supportsNativeFetch()) {
47+
function instrumentFetch(onFetchResolved?: (response: Response) => void): void {
48+
// The native-fetch check is only meaningful in the browser: it probes for `[native code]` and
49+
// falls back to an iframe DOM check to detect a polyfilled/wrapped `fetch` (which we don't want to
50+
// double-instrument alongside XHR). Outside the browser there is no DOM, and `fetch` may be
51+
// legitimately wrapped by the host (e.g. Next.js on Bun), so we always patch the global there.
52+
if (isBrowser() && !supportsNativeFetch()) {
5153
return;
5254
}
5355

0 commit comments

Comments
 (0)