|
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 { createClientReportEnvelope, dsnToString, getGlobalObject, logger, serializeEnvelope } from '@sentry/utils'; |
3 | 4 |
|
4 | 5 | import { eventFromException, eventFromMessage } from './eventbuilder'; |
| 6 | +import { IS_DEBUG_BUILD } from './flags'; |
5 | 7 | import { Breadcrumbs } from './integrations'; |
| 8 | +import { sendReport } from './transports/utils'; |
| 9 | + |
| 10 | +const globalObject = getGlobalObject<Window>(); |
6 | 11 |
|
7 | 12 | export interface BaseBrowserOptions { |
8 | 13 | /** |
@@ -56,7 +61,16 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> { |
56 | 61 | ], |
57 | 62 | version: SDK_VERSION, |
58 | 63 | }; |
| 64 | + |
59 | 65 | 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 | + } |
60 | 74 | } |
61 | 75 |
|
62 | 76 | /** |
@@ -96,4 +110,32 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> { |
96 | 110 | } |
97 | 111 | super._sendEvent(event); |
98 | 112 | } |
| 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 | + } |
99 | 141 | } |
0 commit comments