Skip to content

Commit b3dfde6

Browse files
committed
fix: Remove keepalive from browser
Create only one instance of transport
1 parent a2820a7 commit b3dfde6

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

packages/browser/src/backend.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Backend, logger, Options, SentryError } from '@sentry/core';
2-
import { SentryEvent, SentryEventHint, SentryResponse, Severity, Status } from '@sentry/types';
2+
import { SentryEvent, SentryEventHint, SentryResponse, Severity, Status, Transport } from '@sentry/types';
33
import { isDOMError, isDOMException, isError, isErrorEvent, isPlainObject } from '@sentry/utils/is';
44
import { supportsFetch } from '@sentry/utils/supports';
55
import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers';
@@ -37,6 +37,9 @@ export class BrowserBackend implements Backend {
3737
/** Creates a new browser backend instance. */
3838
public constructor(private readonly options: BrowserOptions = {}) {}
3939

40+
/** Cached transport used internally. */
41+
private transport?: Transport;
42+
4043
/**
4144
* @inheritDoc
4245
*/
@@ -142,15 +145,18 @@ export class BrowserBackend implements Backend {
142145
return { status: Status.Skipped };
143146
}
144147

145-
const transportOptions = this.options.transportOptions ? this.options.transportOptions : { dsn: this.options.dsn };
146-
147-
const transport = this.options.transport
148-
? new this.options.transport({ dsn: this.options.dsn })
149-
: supportsFetch()
150-
? new FetchTransport(transportOptions)
151-
: new XHRTransport(transportOptions);
148+
if (!this.transport) {
149+
const transportOptions = this.options.transportOptions
150+
? this.options.transportOptions
151+
: { dsn: this.options.dsn };
152+
this.transport = this.options.transport
153+
? new this.options.transport({ dsn: this.options.dsn })
154+
: supportsFetch()
155+
? new FetchTransport(transportOptions)
156+
: new XHRTransport(transportOptions);
157+
}
152158

153-
return transport.send(event);
159+
return this.transport.send(event);
154160
}
155161

156162
/**

packages/browser/src/transports/fetch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export class FetchTransport extends BaseTransport {
1414
public async send(event: SentryEvent): Promise<SentryResponse> {
1515
const defaultOptions: RequestInit = {
1616
body: serialize(event),
17-
keepalive: true,
1817
method: 'POST',
1918
// Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default
2019
// https://caniuse.com/#feat=referrer-policy

packages/node/src/backend.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Backend, DSN, Options, SentryError } from '@sentry/core';
22
import { getCurrentHub } from '@sentry/hub';
3-
import { SentryEvent, SentryEventHint, SentryResponse, Severity } from '@sentry/types';
3+
import { SentryEvent, SentryEventHint, SentryResponse, Severity, Transport } from '@sentry/types';
44
import { isError, isPlainObject } from '@sentry/utils/is';
55
import { limitObjectDepthToSize, serializeKeysToEventMessage } from '@sentry/utils/object';
66
import * as md5 from 'md5';
@@ -24,6 +24,9 @@ export class NodeBackend implements Backend {
2424
/** Creates a new Node backend instance. */
2525
public constructor(private readonly options: NodeOptions = {}) {}
2626

27+
/** Cached transport used internally. */
28+
private transport?: Transport;
29+
2730
/**
2831
* @inheritDoc
2932
*/
@@ -93,15 +96,16 @@ export class NodeBackend implements Backend {
9396
dsn = new DSN(this.options.dsn);
9497
}
9598

96-
const transportOptions = this.options.transportOptions ? this.options.transportOptions : { dsn };
97-
98-
const transport = this.options.transport
99-
? new this.options.transport({ dsn })
100-
: dsn.protocol === 'http'
101-
? new HTTPTransport(transportOptions)
102-
: new HTTPSTransport(transportOptions);
99+
if (!this.transport) {
100+
const transportOptions = this.options.transportOptions ? this.options.transportOptions : { dsn };
101+
this.transport = this.options.transport
102+
? new this.options.transport({ dsn })
103+
: dsn.protocol === 'http'
104+
? new HTTPTransport(transportOptions)
105+
: new HTTPSTransport(transportOptions);
106+
}
103107

104-
return transport.send(event);
108+
return this.transport.send(event);
105109
}
106110

107111
/**

0 commit comments

Comments
 (0)