Skip to content

Commit 5e657f0

Browse files
authored
feat!: favor settings instead of explicit dns props (#712)
1 parent a528424 commit 5e657f0

File tree

4 files changed

+17
-49
lines changed

4 files changed

+17
-49
lines changed

packages/use-segment/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"license": "MIT",
2727
"dependencies": {
28-
"@segment/analytics-next": "1.34.0"
28+
"@segment/analytics-next": "1.35.1"
2929
},
3030
"peerDependencies": {
3131
"react": "17.x"

packages/use-segment/src/__tests__/index.tsx

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const wrapper =
4747
onError,
4848
onEventError,
4949
events = defaultEvents,
50-
cdn,
5150
}: Omit<SegmentProviderProps<DefaultEvents>, 'children'>) =>
5251
({ children }: { children: ReactNode }) =>
5352
(
@@ -57,7 +56,6 @@ const wrapper =
5756
onError={onError}
5857
onEventError={onEventError}
5958
events={events}
60-
cdn={cdn}
6159
>
6260
{children}
6361
</SegmentProvider>
@@ -69,9 +67,7 @@ describe('segment hook', () => {
6967
})
7068

7169
it('useSegment should not be defined without SegmentProvider', () => {
72-
const { result } = renderHook(() => useSegment(), {
73-
wrapper: ({ children }) => <div>{children}</div>,
74-
})
70+
const { result } = renderHook(() => useSegment())
7571
expect(() => {
7672
expect(result.current).toBe(undefined)
7773
}).toThrow(Error('useSegment must be used within a SegmentProvider'))
@@ -115,14 +111,12 @@ describe('segment hook', () => {
115111
.spyOn(AnalyticsBrowser, 'load')
116112
.mockResolvedValue([{} as Analytics, {} as Context])
117113

118-
const cdn = 'https://cdn.proxy'
119-
const settings = { writeKey: 'helloworld' }
114+
const settings = { cdn: 'https://cdn.proxy', writeKey: 'helloworld' }
120115

121116
const { result, waitForNextUpdate } = renderHook(
122117
() => useSegment<DefaultEvents>(),
123118
{
124119
wrapper: wrapper({
125-
cdn,
126120
events: defaultEvents,
127121
settings,
128122
}),
@@ -133,7 +127,6 @@ describe('segment hook', () => {
133127
expect(mock).toHaveBeenCalledTimes(1)
134128
expect(mock).toHaveBeenCalledWith(settings, undefined)
135129
expect(result.current.analytics).toStrictEqual({})
136-
expect(window.analytics).toStrictEqual({ _cdn: cdn })
137130
})
138131

139132
it('Provider should load and call onError on analytics load error', async () => {
@@ -232,21 +225,4 @@ describe('segment hook', () => {
232225
// @ts-expect-error if type infering works this should be an error
233226
expect(await result.current.events.pageVisited()).toBe(undefined)
234227
})
235-
236-
it('useSegment should correctly set cdn into windows.analitycs', () => {
237-
// this test should be remove in the same time as this issues is solve
238-
// https://github.com/segmentio/analytics-next/issues/362
239-
240-
const cdn = 'https://cdn.segment.com/analytics.js'
241-
242-
const { result } = renderHook(() => useSegment<DefaultEvents>(), {
243-
wrapper: wrapper({
244-
cdn,
245-
events: defaultEvents,
246-
settings: undefined,
247-
}),
248-
})
249-
250-
expect(result.current.analytics).not.toBeNull()
251-
})
252228
})

packages/use-segment/src/useSegment.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AnalyticsBrowser } from '@segment/analytics-next'
22
import type {
33
Analytics,
4-
AnalyticsSettings,
4+
AnalyticsBrowserSettings,
55
InitOptions,
66
} from '@segment/analytics-next'
77
import {
@@ -41,8 +41,7 @@ export function useSegment<T extends Events>(): SegmentContextInterface<T> {
4141
}
4242

4343
export type SegmentProviderProps<T> = {
44-
cdn?: string
45-
settings?: AnalyticsSettings
44+
settings?: AnalyticsBrowserSettings
4645
initOptions?: InitOptions
4746
onError?: (err: Error) => void
4847
onEventError?: OnEventError
@@ -65,19 +64,11 @@ function SegmentProvider<T extends Events>({
6564
onError,
6665
onEventError,
6766
events,
68-
cdn,
6967
}: SegmentProviderProps<T>) {
7068
const [internalAnalytics, setAnalytics] = useState<Analytics | undefined>(
7169
undefined,
7270
)
7371

74-
if (cdn) {
75-
window.analytics = window.analytics ?? {}
76-
// https://github.com/segmentio/analytics-next/issues/362
77-
// eslint-disable-next-line no-underscore-dangle
78-
window.analytics._cdn = cdn
79-
}
80-
8172
useEffect(() => {
8273
if (settings?.writeKey) {
8374
AnalyticsBrowser.load(settings, initOptions)

pnpm-lock.yaml

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)