Skip to content

Commit

Permalink
chore: Make fetch our default request implementation
Browse files Browse the repository at this point in the history
`fetch` is much more optimized in modern browsers, there shouldn't be any reason not to use it in place of `XMLHTTPRequest` as it's heavily supported. Also, we're still keeping the `XML` fallback if `fetch` doesn't exist.

Closes #1487
  • Loading branch information
rafaeelaudibert committed Dec 16, 2024
1 parent 6f9e089 commit af98f16
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 20 deletions.
2 changes: 0 additions & 2 deletions src/__tests__/featureflags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ describe('featureflags', () => {
expect(instance._send_request).toHaveBeenCalledWith({
url: 'https://us.i.posthog.com/api/early_access_features/?token=random fake token',
method: 'GET',
transport: 'XHR',
callback: expect.any(Function),
})
expect(instance._send_request).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -507,7 +506,6 @@ describe('featureflags', () => {
url: 'https://us.i.posthog.com/api/early_access_features/?token=random fake token',
method: 'GET',
callback: expect.any(Function),
transport: 'XHR',
})
expect(instance._send_request).toHaveBeenCalledTimes(1)

Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/surveys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ describe('surveys', () => {
expect(instance._send_request).toHaveBeenCalledWith({
url: 'https://us.i.posthog.com/api/surveys/?token=testtoken',
method: 'GET',
transport: 'XHR',
callback: expect.any(Function),
})
expect(instance._send_request).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -281,7 +280,6 @@ describe('surveys', () => {
expect(instance._send_request).toHaveBeenCalledWith({
url: 'https://us.i.posthog.com/api/surveys/?token=testtoken',
method: 'GET',
transport: 'XHR',
callback: expect.any(Function),
})
expect(instance._send_request).toHaveBeenCalledTimes(1)
Expand Down
10 changes: 5 additions & 5 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ export class PostHog {
this.compression = includes(config['supportedCompression'], Compression.GZipJS)
? Compression.GZipJS
: includes(config['supportedCompression'], Compression.Base64)
? Compression.Base64
: undefined
? Compression.Base64
: undefined
}

if (config.analytics?.endpoint) {
Expand All @@ -587,8 +587,8 @@ export class PostHog {
person_profiles: this._initialPersonProfilesConfig
? this._initialPersonProfilesConfig
: config['defaultIdentifiedOnly']
? 'identified_only'
: 'always',
? 'identified_only'
: 'always',
})

this.siteApps?.onRemoteConfig(config)
Expand Down Expand Up @@ -1710,7 +1710,7 @@ export class PostHog {
* // Capture rage clicks
* rageclick: true
*
* // transport for sending requests ('XHR' or 'sendBeacon')
* // transport for sending requests ('XHR' | 'fetch' | 'sendBeacon')
* // NB: sendBeacon should only be used for scenarios such as
* // page unload where a "best-effort" attempt to send is
* // acceptable; the sendBeacon API does not support callbacks
Expand Down
1 change: 0 additions & 1 deletion src/posthog-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ export class PostHogFeatureFlags {

if (!existing_early_access_features || force_reload) {
this.instance._send_request({
transport: 'XHR',
url: this.instance.requestRouter.endpointFor(
'api',
`/api/early_access_features/?token=${this.instance.config.token}`
Expand Down
1 change: 0 additions & 1 deletion src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export class PostHogSurveys {
`/api/surveys/?token=${this.instance.config.token}`
),
method: 'GET',
transport: 'XHR',
callback: (response) => {
if (response.statusCode !== 200 || !response.json) {
return callback([])
Expand Down
15 changes: 7 additions & 8 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,17 @@ const _sendBeacon = (options: RequestOptions) => {
const AVAILABLE_TRANSPORTS: { transport: RequestOptions['transport']; method: (options: RequestOptions) => void }[] = []

// We add the transports in order of preference

if (XMLHttpRequest) {
if (fetch) {
AVAILABLE_TRANSPORTS.push({
transport: 'XHR',
method: xhr,
transport: 'fetch',
method: _fetch,
})
}

if (fetch) {
if (XMLHttpRequest) {
AVAILABLE_TRANSPORTS.push({
transport: 'fetch',
method: _fetch,
transport: 'XHR',
method: xhr,
})
}

Expand All @@ -250,7 +249,7 @@ export const request = (_options: RequestOptions) => {
compression: options.compression,
})

const transport = options.transport ?? 'XHR'
const transport = options.transport ?? 'fetch'

const transportMethod =
find(AVAILABLE_TRANSPORTS, (t) => t.transport === transport)?.method ?? AVAILABLE_TRANSPORTS[0].method
Expand Down
1 change: 0 additions & 1 deletion src/web-experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export class WebExperiments {
`/api/web_experiments/?token=${this.instance.config.token}`
),
method: 'GET',
transport: 'XHR',
callback: (response) => {
if (response.statusCode !== 200 || !response.json) {
return callback([])
Expand Down

0 comments on commit af98f16

Please sign in to comment.