Skip to content
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
44 changes: 43 additions & 1 deletion packages/browser/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
import { createClientReportEnvelope, dsnToString, getGlobalObject, logger, serializeEnvelope } from '@sentry/utils';

import { eventFromException, eventFromMessage } from './eventbuilder';
import { IS_DEBUG_BUILD } from './flags';
import { Breadcrumbs } from './integrations';
import { sendReport } from './transports/utils';

const globalObject = getGlobalObject<Window>();

export interface BaseBrowserOptions {
/**
Expand Down Expand Up @@ -56,7 +61,16 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
],
version: SDK_VERSION,
};

super(options);

if (options.sendClientReports && globalObject.document) {
globalObject.document.addEventListener('visibilitychange', () => {
if (globalObject.document.visibilityState === 'hidden') {
this._flushOutcomes();
}
});
}
}

/**
Expand Down Expand Up @@ -96,4 +110,32 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
}
super._sendEvent(event);
}

/**
* Sends client reports as an envelope.
*/
private _flushOutcomes(): void {
const outcomes = this._clearOutcomes();

if (outcomes.length === 0) {
IS_DEBUG_BUILD && logger.log('No outcomes to send');
return;
}

if (!this._dsn) {
IS_DEBUG_BUILD && logger.log('No dsn provided, will not send outcomes');
return;
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was like this before but I'm very unsure whether the && in this line should actually be a ||.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was based on this logic:

...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),


try {
sendReport(url, serializeEnvelope(envelope));
} catch (e) {
IS_DEBUG_BUILD && logger.error(e);
}
}
}
6 changes: 2 additions & 4 deletions packages/browser/src/transports/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ export function sendReport(url: string, body: string): void {
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);
return sendBeacon(url, body);
}

if (supportsFetch()) {
sendBeacon(url, body);
} else if (supportsFetch()) {
const fetch = getNativeFetchImplementation();
fetch(url, {
body,
Expand Down