Skip to content

ref(browser): Use configured transport as fallback for client reports #5797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { eventFromException, eventFromMessage } from './eventbuilder';
import { Breadcrumbs } from './integrations';
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
import { BrowserTransportOptions } from './transports/types';
import { sendReport } from './transports/utils';

const globalObject = getGlobalObject<Window>();

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

try {
sendReport(url, serializeEnvelope(envelope));
const global = getGlobalObject<Window>();
const isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
const hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';
// Make sure beacon is not used if user configures custom transport options
if (hasSendBeacon && !this._options.transportOptions) {
// Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
const sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
sendBeacon(url, serializeEnvelope(envelope));
} else {
// If beacon is not supported or if they are using the tunnel option
// use our regular transport to send client reports to Sentry.
this._sendEnvelope(envelope);
}
} catch (e) {
__DEBUG_BUILD__ && logger.error(e);
}
Expand Down
29 changes: 1 addition & 28 deletions packages/browser/src/transports/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils';
import { getGlobalObject, isNativeFetch, logger } from '@sentry/utils';

const global = getGlobalObject<Window>();
let cachedFetchImpl: FetchImpl;
Expand Down Expand Up @@ -77,30 +77,3 @@ export function getNativeFetchImplementation(): FetchImpl {
return (cachedFetchImpl = fetchImpl.bind(global));
/* eslint-enable @typescript-eslint/unbound-method */
}

/**
* Sends sdk client report using sendBeacon or fetch as a fallback if available
*
* @param url report endpoint
* @param body report payload
*/
export function sendReport(url: string, body: string | Uint8Array): void {
const isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]';
const hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function';

if (hasSendBeacon) {
// Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
const sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
sendBeacon(url, body);
} else if (supportsFetch()) {
const fetch = getNativeFetchImplementation();
fetch(url, {
body,
method: 'POST',
credentials: 'omit',
keepalive: true,
}).then(null, error => {
__DEBUG_BUILD__ && logger.error(error);
});
}
}