Skip to content

Commit acc9cb1

Browse files
authored
ref(browser): use configured transport as fallback for client reports (#5797)
Update the client report sending logic to fallback on whatever transport is initialized on the client. This fallback is also used when a user configures custom transport options.
1 parent d6bcb47 commit acc9cb1

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

packages/browser/src/client.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { eventFromException, eventFromMessage } from './eventbuilder';
1313
import { Breadcrumbs } from './integrations';
1414
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
1515
import { BrowserTransportOptions } from './transports/types';
16-
import { sendReport } from './transports/utils';
1716

1817
const globalObject = getGlobalObject<Window>();
1918

@@ -165,7 +164,19 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
165164
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
166165

167166
try {
168-
sendReport(url, serializeEnvelope(envelope));
167+
const global = getGlobalObject<Window>();
168+
const isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
169+
const hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
170+
// Make sure beacon is not used if user configures custom transport options
171+
if (hasSendBeacon && !this._options.transportOptions) {
172+
// Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
173+
const sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
174+
sendBeacon(url, serializeEnvelope(envelope));
175+
} else {
176+
// If beacon is not supported or if they are using the tunnel option
177+
// use our regular transport to send client reports to Sentry.
178+
this._sendEnvelope(envelope);
179+
}
169180
} catch (e) {
170181
__DEBUG_BUILD__ && logger.error(e);
171182
}

packages/browser/src/transports/utils.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
1+
import { getGlobalObject, isNativeFetch, logger } from '@sentry/utils';
22

33
const global = getGlobalObject<Window>();
44
let cachedFetchImpl: FetchImpl;
@@ -77,30 +77,3 @@ export function getNativeFetchImplementation(): FetchImpl {
7777
return (cachedFetchImpl = fetchImpl.bind(global));
7878
/* eslint-enable @typescript-eslint/unbound-method */
7979
}
80-
81-
/**
82-
* Sends sdk client report using sendBeacon or fetch as a fallback if available
83-
*
84-
* @param url report endpoint
85-
* @param body report payload
86-
*/
87-
export function sendReport(url: string, body: string | Uint8Array): void {
88-
const isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
89-
const hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
90-
91-
if (hasSendBeacon) {
92-
// Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
93-
const sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
94-
sendBeacon(url, body);
95-
} else if (supportsFetch()) {
96-
const fetch = getNativeFetchImplementation();
97-
fetch(url, {
98-
body,
99-
method: 'POST',
100-
credentials: 'omit',
101-
keepalive: true,
102-
}).then(null, error => {
103-
__DEBUG_BUILD__ && logger.error(error);
104-
});
105-
}
106-
}

0 commit comments

Comments
 (0)