|
1 |
| -import { BaseClient, Scope, SDK_VERSION } from '@sentry/core'; |
| 1 | +import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core'; |
2 | 2 | import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
|
3 |
| -import { getGlobalObject, logger } from '@sentry/utils'; |
| 3 | +import { createClientReportEnvelope, dsnToString, getGlobalObject, logger, serializeEnvelope } from '@sentry/utils'; |
4 | 4 |
|
5 | 5 | import { eventFromException, eventFromMessage } from './eventbuilder';
|
6 | 6 | import { IS_DEBUG_BUILD } from './flags';
|
7 | 7 | import { injectReportDialog, ReportDialogOptions } from './helpers';
|
8 | 8 | import { Breadcrumbs } from './integrations';
|
| 9 | +import { sendReport } from './transports/utils'; |
| 10 | + |
| 11 | +const globalObject = getGlobalObject<Window>(); |
9 | 12 |
|
10 | 13 | export interface BaseBrowserOptions {
|
11 | 14 | /**
|
@@ -59,7 +62,16 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
|
59 | 62 | ],
|
60 | 63 | version: SDK_VERSION,
|
61 | 64 | };
|
| 65 | + |
62 | 66 | super(options);
|
| 67 | + |
| 68 | + if (options.sendClientReports && globalObject.document) { |
| 69 | + globalObject.document.addEventListener('visibilitychange', () => { |
| 70 | + if (globalObject.document.visibilityState === 'hidden') { |
| 71 | + this._flushOutcomes(); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
63 | 75 | }
|
64 | 76 |
|
65 | 77 | /**
|
@@ -122,4 +134,32 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
|
122 | 134 | }
|
123 | 135 | super._sendEvent(event);
|
124 | 136 | }
|
| 137 | + |
| 138 | + /** |
| 139 | + * Sends client reports as an envelope. |
| 140 | + */ |
| 141 | + private _flushOutcomes(): void { |
| 142 | + const outcomes = this._clearOutcomes(); |
| 143 | + |
| 144 | + if (outcomes.length === 0) { |
| 145 | + IS_DEBUG_BUILD && logger.log('No outcomes to send'); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + if (!this._dsn) { |
| 150 | + IS_DEBUG_BUILD && logger.log('No dsn provided, will not send outcomes'); |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + IS_DEBUG_BUILD && logger.log(`Sending outcomes:\n${JSON.stringify(outcomes, null, 2)}`); |
| 155 | + |
| 156 | + const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, this._options.tunnel); |
| 157 | + const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn)); |
| 158 | + |
| 159 | + try { |
| 160 | + sendReport(url, serializeEnvelope(envelope)); |
| 161 | + } catch (e) { |
| 162 | + IS_DEBUG_BUILD && logger.error(e); |
| 163 | + } |
| 164 | + } |
125 | 165 | }
|
0 commit comments