Skip to content

Commit d82bda4

Browse files
committed
add additional headers
1 parent c3dbd52 commit d82bda4

File tree

4 files changed

+70
-29
lines changed

4 files changed

+70
-29
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import { onPageChange } from '../../lib/on-page-change'
44
import { SegmentFacade } from '../../lib/to-facade'
55
import { RateLimitError } from './ratelimit-error'
66
import { Context } from '../../core/context'
7-
8-
export type BatchingDispatchConfig = {
9-
size?: number
10-
timeout?: number
11-
maxRetries?: number
12-
keepalive?: boolean
13-
}
7+
import { BatchingDispatchConfig, createHeaders } from './shared-dispatcher'
148

159
const MAX_PAYLOAD_SIZE = 500
1610
const MAX_KEEPALIVE_SIZE = 64
@@ -84,9 +78,8 @@ export default function batch(
8478

8579
return fetch(`https://${apiHost}/b`, {
8680
keepalive: config?.keepalive || pageUnloaded,
87-
headers: {
88-
'Content-Type': 'text/plain',
89-
},
81+
headers: createHeaders(config?.additionalHeaders),
82+
...(config?.fetchPriority ? { priority: config?.fetchPriority } : {}),
9083
method: 'post',
9184
body: JSON.stringify({
9285
writeKey,

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import { fetch } from '../../lib/fetch'
22
import { RateLimitError } from './ratelimit-error'
3-
3+
import { createHeaders, StandardDispatcherConfig } from './shared-dispatcher'
44
export type Dispatcher = (url: string, body: object) => Promise<unknown>
55

6-
export type StandardDispatcherConfig = {
7-
keepalive?: boolean
8-
}
9-
106
export default function (config?: StandardDispatcherConfig): {
117
dispatch: Dispatcher
128
} {
139
function dispatch(url: string, body: object): Promise<unknown> {
1410
return fetch(url, {
1511
keepalive: config?.keepalive,
16-
headers: { 'Content-Type': 'text/plain' },
12+
headers: createHeaders(config?.additionalHeaders),
1713
method: 'post',
1814
body: JSON.stringify(body),
15+
...(config?.fetchPriority ? { priority: config?.fetchPriority } : {}),
1916
}).then((res) => {
2017
if (res.status >= 500) {
2118
throw new Error(`Bad response from server: ${res.status}`)

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,12 @@ 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, { BatchingDispatchConfig } from './batched-dispatcher'
11-
import standard, { StandardDispatcherConfig } from './fetch-dispatcher'
10+
import batch from './batched-dispatcher'
11+
import standard from './fetch-dispatcher'
1212
import { normalize } from './normalize'
1313
import { scheduleFlush } from './schedule-flush'
1414
import { SEGMENT_API_HOST } from '../../core/constants'
15-
16-
type DeliveryStrategy =
17-
| {
18-
strategy?: 'standard'
19-
config?: StandardDispatcherConfig
20-
}
21-
| {
22-
strategy?: 'batching'
23-
config?: BatchingDispatchConfig
24-
}
15+
import { DeliveryStrategy } from './shared-dispatcher'
2516

2617
export type SegmentioSettings = {
2718
apiKey: string
@@ -95,7 +86,7 @@ export function segmentio(
9586
const client =
9687
deliveryStrategy?.strategy === 'batching'
9788
? batch(apiHost, deliveryStrategy.config)
98-
: standard(deliveryStrategy?.config as StandardDispatcherConfig)
89+
: standard(deliveryStrategy?.config)
9990

10091
async function send(ctx: Context): Promise<Context> {
10192
if (isOffline()) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export const createHeaders = (
2+
headerSettings: AdditionalHeaders | undefined
3+
): Record<string, string> => {
4+
return {
5+
'Content-Type': 'text/plain',
6+
...(typeof headerSettings === 'function'
7+
? headerSettings()
8+
: headerSettings),
9+
}
10+
}
11+
12+
/**
13+
* Additional headers to be sent with the request.
14+
* Default is `Content-Type: text/plain`. This can be overridden.
15+
* If a function is provided, it will be called before each request.
16+
*/
17+
export type AdditionalHeaders =
18+
| Record<string, string>
19+
| (() => Record<string, string>)
20+
21+
/**
22+
* Priority of the request.
23+
* chrome only
24+
* @default 'auto'
25+
*/
26+
export type FetchPriority = 'high' | 'low' | 'auto'
27+
28+
export type BatchingDispatchConfig = {
29+
size?: number
30+
timeout?: number
31+
maxRetries?: number
32+
additionalHeaders?: AdditionalHeaders
33+
/**
34+
* This is useful for ensuring that events are sent even if the user navigates away from the page.
35+
* @default false IF the page is being unloaded, true otherwise
36+
*/
37+
keepalive?: boolean
38+
fetchPriority?: FetchPriority
39+
}
40+
41+
export type StandardDispatcherConfig = {
42+
/**
43+
* This is useful for ensuring that an event is sent even if the user navigates away from the page.
44+
* However, it may increase the likelihood of events being lost, as there is a 64kb limit for all fetch requests with keepalive (which is why it's disabled by default).
45+
* @default false
46+
*/
47+
keepalive?: boolean
48+
additionalHeaders?: AdditionalHeaders
49+
fetchPriority?: FetchPriority
50+
}
51+
52+
export type DeliveryStrategy =
53+
| {
54+
strategy?: 'standard'
55+
config?: StandardDispatcherConfig
56+
}
57+
| {
58+
strategy?: 'batching'
59+
config?: BatchingDispatchConfig
60+
}

0 commit comments

Comments
 (0)