Skip to content

Commit 8a5db68

Browse files
authored
fix(browser): Send client outcomes through tunnel if configured (#4031)
1 parent 24ab018 commit 8a5db68

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/browser/src/transports/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export abstract class BaseTransport implements Transport {
114114

115115
const url = this._api.getEnvelopeEndpointWithUrlEncodedAuth();
116116
// Envelope header is required to be at least an empty object
117-
const envelopeHeader = JSON.stringify({});
117+
const envelopeHeader = JSON.stringify({ ...(this.options.tunnel && { dsn: this._api.getDsn().toString() }) });
118118
const itemHeaders = JSON.stringify({
119119
type: 'client_report',
120120
});

packages/browser/test/unit/transports/base.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ describe('BaseTransport', () => {
9292
`{}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(outcomes)}}`,
9393
);
9494
});
95+
96+
it('attaches DSN to envelope header if tunnel is configured', () => {
97+
const tunnel = 'https://hello.com/world';
98+
const transport = new SimpleTransport({ dsn: testDsn, sendClientReports: true, tunnel });
99+
100+
transport.recordLostEvent(Outcome.BeforeSend, 'event');
101+
102+
visibilityState = 'hidden';
103+
document.dispatchEvent(new Event('visibilitychange'));
104+
105+
const outcomes = [{ reason: Outcome.BeforeSend, category: 'error', quantity: 1 }];
106+
107+
expect(sendBeaconSpy).toHaveBeenCalledWith(
108+
tunnel,
109+
`{"dsn":"${testDsn}"}\n{"type":"client_report"}\n{"timestamp":12.345,"discarded_events":${JSON.stringify(
110+
outcomes,
111+
)}}`,
112+
);
113+
});
95114
});
96115

97116
it('doesnt provide sendEvent() implementation', () => {

0 commit comments

Comments
 (0)