|
1 | 1 | import { eventToSentryRequest, sessionToSentryRequest } from '@sentry/core';
|
2 | 2 | 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'; |
4 | 4 |
|
5 | 5 | import { BaseTransport } from './base';
|
6 | 6 |
|
@@ -45,24 +45,33 @@ type FetchImpl = typeof fetch;
|
45 | 45 | * Safari: resource blocked by content blocker
|
46 | 46 | */
|
47 | 47 | function getNativeFetchImplementation(): FetchImpl {
|
48 |
| - // Make sure that the fetch we use is always the native one. |
| 48 | + /* eslint-disable @typescript-eslint/unbound-method */ |
| 49 | + |
| 50 | + // Fast path to avoid DOM I/O |
49 | 51 | const global = getGlobalObject<Window>();
|
| 52 | + if (isNativeFetch(global.fetch)) { |
| 53 | + return global.fetch.bind(global); |
| 54 | + } |
| 55 | + |
50 | 56 | const document = global.document;
|
| 57 | + let fetchImpl = global.fetch; |
51 | 58 | // eslint-disable-next-line deprecation/deprecation
|
52 | 59 | if (typeof document?.createElement === `function`) {
|
53 | 60 | try {
|
54 | 61 | const sandbox = document.createElement('iframe');
|
55 | 62 | sandbox.hidden = true;
|
56 | 63 | document.head.appendChild(sandbox);
|
57 | 64 | if (sandbox.contentWindow?.fetch) {
|
58 |
| - return sandbox.contentWindow.fetch.bind(global); |
| 65 | + fetchImpl = sandbox.contentWindow.fetch; |
59 | 66 | }
|
60 | 67 | document.head.removeChild(sandbox);
|
61 | 68 | } catch (e) {
|
62 | 69 | logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);
|
63 | 70 | }
|
64 | 71 | }
|
65 |
| - return global.fetch.bind(global); |
| 72 | + |
| 73 | + return fetchImpl.bind(global); |
| 74 | + /* eslint-enable @typescript-eslint/unbound-method */ |
66 | 75 | }
|
67 | 76 |
|
68 | 77 | /** `fetch` based transport */
|
|
0 commit comments