Skip to content

Commit f8c64a3

Browse files
lforstAbhiPrasad
authored andcommitted
ref: Add back client reporting to BrowserClient (#5018)
1 parent 9871829 commit f8c64a3

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

packages/browser/src/client.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
1+
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
22
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
3+
import { createClientReportEnvelope, dsnToString, getGlobalObject, logger, serializeEnvelope } from '@sentry/utils';
34

45
import { eventFromException, eventFromMessage } from './eventbuilder';
6+
import { IS_DEBUG_BUILD } from './flags';
57
import { Breadcrumbs } from './integrations';
8+
import { sendReport } from './transports/utils';
9+
10+
const globalObject = getGlobalObject<Window>();
611

712
export interface BaseBrowserOptions {
813
/**
@@ -56,7 +61,16 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
5661
],
5762
version: SDK_VERSION,
5863
};
64+
5965
super(options);
66+
67+
if (options.sendClientReports && globalObject.document) {
68+
globalObject.document.addEventListener('visibilitychange', () => {
69+
if (globalObject.document.visibilityState === 'hidden') {
70+
this._flushOutcomes();
71+
}
72+
});
73+
}
6074
}
6175

6276
/**
@@ -96,4 +110,32 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
96110
}
97111
super._sendEvent(event);
98112
}
113+
114+
/**
115+
* Sends client reports as an envelope.
116+
*/
117+
private _flushOutcomes(): void {
118+
const outcomes = this._clearOutcomes();
119+
120+
if (outcomes.length === 0) {
121+
IS_DEBUG_BUILD && logger.log('No outcomes to send');
122+
return;
123+
}
124+
125+
if (!this._dsn) {
126+
IS_DEBUG_BUILD && logger.log('No dsn provided, will not send outcomes');
127+
return;
128+
}
129+
130+
IS_DEBUG_BUILD && logger.log('Sending outcomes:', outcomes);
131+
132+
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, this._options.tunnel);
133+
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
134+
135+
try {
136+
sendReport(url, serializeEnvelope(envelope));
137+
} catch (e) {
138+
IS_DEBUG_BUILD && logger.error(e);
139+
}
140+
}
99141
}

packages/browser/src/transports/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ export function sendReport(url: string, body: string): void {
9393
if (hasSendBeacon) {
9494
// Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
9595
const sendBeacon = global.navigator.sendBeacon.bind(global.navigator);
96-
return sendBeacon(url, body);
97-
}
98-
99-
if (supportsFetch()) {
96+
sendBeacon(url, body);
97+
} else if (supportsFetch()) {
10098
const fetch = getNativeFetchImplementation();
10199
fetch(url, {
102100
body,

0 commit comments

Comments
 (0)