Skip to content

Commit

Permalink
Fix webdriver.io interception issue by getting fetch function at call…
Browse files Browse the repository at this point in the history
… time rather than import time
  • Loading branch information
silesky committed Dec 29, 2022
1 parent d8af6fc commit e7b9761
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-foxes-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fix webdriver.io interception bug. Refactor to use native fetch where unfetch is unavailable.
2 changes: 1 addition & 1 deletion packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getProcessEnv } from '../lib/get-process-env'
import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn'

import fetch from 'unfetch'
import { fetch } from '../lib/fetch'
import { Analytics, AnalyticsSettings, InitOptions } from '../core/analytics'
import { Context } from '../core/context'
import { Plan } from '../core/events'
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/core/stats/remote-metrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'unfetch'
import { fetch } from '../../lib/fetch'
import { version } from '../../generated/version'
import { getVersionType } from '../../plugins/segmentio/normalize'

Expand Down
15 changes: 15 additions & 0 deletions packages/browser/src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unfetch from 'unfetch'
import { getGlobal } from './get-global'

/**
* Wrapper around native `fetch` containing `unfetch` fallback.
*/
export const fetch: typeof unfetch = (...args) => {
const global = getGlobal()
let fetch = unfetch

if (global && global.fetch) {
fetch = global.fetch
}
return fetch(...args)
}
8 changes: 1 addition & 7 deletions packages/browser/src/plugins/segmentio/batched-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import unfetch from 'unfetch'
import { SegmentEvent } from '../../core/events'
import { fetch } from '../../lib/fetch'
import { onPageLeave } from '../../lib/on-page-leave'

let fetch = unfetch
if (typeof window !== 'undefined') {
fetch = window.fetch || unfetch
}

type BatchingConfig = {
size?: number
timeout?: number
Expand Down Expand Up @@ -61,7 +56,6 @@ export default function batch(apiHost: string, config?: BatchingConfig) {
}

const writeKey = (batch[0] as SegmentEvent)?.writeKey

return fetch(`https://${apiHost}/b`, {
keepalive: pageUnloaded,
headers: {
Expand Down
7 changes: 1 addition & 6 deletions packages/browser/src/plugins/segmentio/fetch-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import unfetch from 'unfetch'

import { fetch } from '../../lib/fetch'
export type Dispatcher = (url: string, body: object) => Promise<unknown>

export default function (): { dispatch: Dispatcher } {
function dispatch(url: string, body: object): Promise<unknown> {
let fetch = unfetch
if (typeof window !== 'undefined') {
fetch = window.fetch || unfetch
}
return fetch(url, {
headers: { 'Content-Type': 'text/plain' },
method: 'post',
Expand Down

0 comments on commit e7b9761

Please sign in to comment.