Skip to content

ref(browser): Remove sendBeacon API usage #7552

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 2 commits into from
Mar 21, 2023
Merged
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
23 changes: 3 additions & 20 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Scope } from '@sentry/core';
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, SDK_VERSION } from '@sentry/core';
import { BaseClient, SDK_VERSION } from '@sentry/core';
import type {
BrowserClientReplayOptions,
ClientOptions,
Expand All @@ -9,7 +9,7 @@ import type {
Severity,
SeverityLevel,
} from '@sentry/types';
import { createClientReportEnvelope, dsnToString, getSDKSource, logger, serializeEnvelope } from '@sentry/utils';
import { createClientReportEnvelope, dsnToString, getSDKSource, logger } from '@sentry/utils';

import { eventFromException, eventFromMessage } from './eventbuilder';
import { WINDOW } from './helpers';
Expand Down Expand Up @@ -132,24 +132,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {

__DEBUG_BUILD__ && logger.log('Sending outcomes:', outcomes);

const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, this._options);
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));

try {
const isRealNavigator = Object.prototype.toString.call(WINDOW && WINDOW.navigator) === '[object Navigator]';
const hasSendBeacon = isRealNavigator && typeof WINDOW.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 = WINDOW.navigator.sendBeacon.bind(WINDOW.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.
void this._sendEnvelope(envelope);
}
} catch (e) {
__DEBUG_BUILD__ && logger.error(e);
}
void this._sendEnvelope(envelope);
}
}