Skip to content

Commit 6fbae8d

Browse files
authored
Make keepalive configurable and default to false (#788)
1 parent 9ec9cb1 commit 6fbae8d

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

.changeset/tasty-badgers-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Make keep-alive configurable and default to false

packages/browser/src/plugins/segmentio/__tests__/index.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,40 @@ describe('Segment.io', () => {
6565
})
6666
})
6767

68+
describe('configuring a keep alive', () => {
69+
it('should accept keepalive configuration', async () => {
70+
const analytics = new Analytics({ writeKey: 'foo' })
71+
72+
await analytics.register(
73+
segmentio(analytics, {
74+
apiKey: '',
75+
deliveryStrategy: {
76+
config: {
77+
keepalive: true,
78+
},
79+
},
80+
})
81+
)
82+
83+
await analytics.track('foo')
84+
const [_, params] = spyMock.mock.lastCall
85+
expect(params.keepalive).toBe(true)
86+
})
87+
88+
it('should default to no keepalive', async () => {
89+
const analytics = new Analytics({ writeKey: 'foo' })
90+
91+
const segment = segmentio(analytics, {
92+
apiKey: '',
93+
})
94+
await analytics.register(segment)
95+
await analytics.track('foo')
96+
97+
const [_, params] = spyMock.mock.lastCall
98+
expect(params.keepalive).toBeUndefined()
99+
})
100+
})
101+
68102
describe('#page', () => {
69103
it('should enqueue section, name and properties', async () => {
70104
await analytics.page('section', 'name', { property: true }, { opt: true })

packages/browser/src/plugins/segmentio/batched-dispatcher.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SegmentEvent } from '../../core/events'
22
import { fetch } from '../../lib/fetch'
33
import { onPageLeave } from '../../lib/on-page-leave'
44

5-
type BatchingConfig = {
5+
export type BatchingDispatchConfig = {
66
size?: number
77
timeout?: number
88
}
@@ -43,7 +43,10 @@ function chunks(batch: object[]): Array<object[]> {
4343
return result
4444
}
4545

46-
export default function batch(apiHost: string, config?: BatchingConfig) {
46+
export default function batch(
47+
apiHost: string,
48+
config?: BatchingDispatchConfig
49+
) {
4750
let buffer: object[] = []
4851
let pageUnloaded = false
4952

packages/browser/src/plugins/segmentio/fetch-dispatcher.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import { fetch } from '../../lib/fetch'
22

33
export type Dispatcher = (url: string, body: object) => Promise<unknown>
44

5-
export default function (): { dispatch: Dispatcher } {
5+
export type StandardDispatcherConfig = {
6+
keepalive?: boolean
7+
}
8+
9+
export default function (config?: StandardDispatcherConfig): {
10+
dispatch: Dispatcher
11+
} {
612
function dispatch(url: string, body: object): Promise<unknown> {
713
return fetch(url, {
8-
keepalive: true,
14+
keepalive: config?.keepalive,
915
headers: { 'Content-Type': 'text/plain' },
1016
method: 'post',
1117
body: JSON.stringify(body),

packages/browser/src/plugins/segmentio/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ import { Plugin } from '../../core/plugin'
77
import { PriorityQueue } from '../../lib/priority-queue'
88
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
99
import { toFacade } from '../../lib/to-facade'
10-
import batch from './batched-dispatcher'
11-
import standard from './fetch-dispatcher'
10+
import batch, { BatchingDispatchConfig } from './batched-dispatcher'
11+
import standard, { StandardDispatcherConfig } from './fetch-dispatcher'
1212
import { normalize } from './normalize'
1313
import { scheduleFlush } from './schedule-flush'
1414

15+
type DeliveryStrategy =
16+
| {
17+
strategy?: 'standard'
18+
config?: StandardDispatcherConfig
19+
}
20+
| {
21+
strategy?: 'batching'
22+
config?: BatchingDispatchConfig
23+
}
24+
1525
export type SegmentioSettings = {
1626
apiKey: string
1727
apiHost?: string
@@ -24,13 +34,7 @@ export type SegmentioSettings = {
2434

2535
maybeBundledConfigIds?: Record<string, string[]>
2636

27-
deliveryStrategy?: {
28-
strategy?: 'standard' | 'batching'
29-
config?: {
30-
size?: number
31-
timeout?: number
32-
}
33-
}
37+
deliveryStrategy?: DeliveryStrategy
3438
}
3539

3640
type JSON = ReturnType<Facade['json']>
@@ -71,10 +75,11 @@ export function segmentio(
7175
const protocol = settings?.protocol ?? 'https'
7276
const remote = `${protocol}://${apiHost}`
7377

78+
const deliveryStrategy = settings?.deliveryStrategy
7479
const client =
75-
settings?.deliveryStrategy?.strategy === 'batching'
76-
? batch(apiHost, settings?.deliveryStrategy?.config)
77-
: standard()
80+
deliveryStrategy?.strategy === 'batching'
81+
? batch(apiHost, deliveryStrategy.config)
82+
: standard(deliveryStrategy?.config as StandardDispatcherConfig)
7883

7984
async function send(ctx: Context): Promise<Context> {
8085
if (isOffline()) {

0 commit comments

Comments
 (0)