Skip to content

Commit 3f58366

Browse files
authored
CDN URL on Analytics Settings (#1157)
1 parent 9182880 commit 3f58366

File tree

6 files changed

+19
-9
lines changed

6 files changed

+19
-9
lines changed

.changeset/angry-items-change.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+
Refactoring CDN Url to be available from analytics settings

packages/browser/src/browser/__tests__/integration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ describe('public settings api', () => {
10451045
writeKey,
10461046
cdnSettings: cdnSettingsMinimal,
10471047
timeout: 300,
1048+
cdnURL: 'https://cdn.segment.com',
10481049
})
10491050
})
10501051

packages/browser/src/browser/__tests__/standalone-analytics.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ describe('standalone bundle', () => {
132132
// @ts-ignore ignore Response required fields
133133
.mockImplementation((): Promise<Response> => fetchSettings)
134134

135-
await loadCDNSettings(segmentDotCom)
135+
const mockCdn = 'https://cdn.foo.com'
136+
137+
await loadCDNSettings(segmentDotCom, mockCdn)
136138

137139
expect(unfetch).toHaveBeenCalledWith(
138140
'https://cdn.foo.com/v1/projects/foo/settings'
@@ -147,7 +149,7 @@ describe('standalone bundle', () => {
147149
const mockCdn = 'http://my-overridden-cdn.com'
148150

149151
getGlobalAnalytics()!._cdn = mockCdn
150-
await loadCDNSettings(segmentDotCom)
152+
await loadCDNSettings(segmentDotCom, mockCdn)
151153

152154
expect(unfetch).toHaveBeenCalledWith(expect.stringContaining(mockCdn))
153155
})

packages/browser/src/browser/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ export interface AnalyticsBrowserSettings {
148148

149149
export function loadCDNSettings(
150150
writeKey: string,
151-
cdnURL?: string
151+
baseUrl: string
152152
): Promise<CDNSettings> {
153-
const baseUrl = cdnURL ?? getCDN()
154-
155153
return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
156154
.then((res) => {
157155
if (!res.ok) {
@@ -355,9 +353,9 @@ async function loadAnalytics(
355353
preInitBuffer.add(new PreInitMethodCall('page', []))
356354
}
357355

356+
const cdnURL = settings.cdnURL ?? getCDN()
358357
let cdnSettings =
359-
settings.cdnSettings ??
360-
(await loadCDNSettings(settings.writeKey, settings.cdnURL))
358+
settings.cdnSettings ?? (await loadCDNSettings(settings.writeKey, cdnURL))
361359

362360
if (options.updateCDNSettings) {
363361
cdnSettings = options.updateCDNSettings(cdnSettings)
@@ -379,7 +377,7 @@ async function loadAnalytics(
379377
...options,
380378
}
381379

382-
const analytics = new Analytics({ ...settings, cdnSettings }, options)
380+
const analytics = new Analytics({ ...settings, cdnSettings, cdnURL }, options)
383381

384382
attachInspector(analytics)
385383

packages/browser/src/core/analytics/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ export class AnalyticsInstanceSettings {
8080
* This is an unstable API, it may change in the future without warning.
8181
*/
8282
readonly cdnSettings: CDNSettings
83+
readonly cdnURL?: string
8384

8485
/**
85-
* Auto-track specific timeout setting for legacy purposes.
86+
* Auto-track specific timeout setting for legacy purposes.
8687
*/
8788
timeout = 300
8889

@@ -92,6 +93,7 @@ export class AnalyticsInstanceSettings {
9293
integrations: {},
9394
edgeFunction: {},
9495
}
96+
this.cdnURL = settings.cdnURL
9597
}
9698
}
9799

@@ -101,6 +103,7 @@ export class AnalyticsInstanceSettings {
101103
export interface AnalyticsSettings {
102104
writeKey: string
103105
cdnSettings?: CDNSettings
106+
cdnURL?: string
104107
}
105108

106109
export interface InitOptions {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('Segment.io retries 500s and 429', () => {
5252
})
5353

5454
test('delays retry on 429', async () => {
55+
jest.useFakeTimers({ advanceTimers: true })
5556
const headers = new Headers()
5657
const resetTime = 1234
5758
headers.set('x-ratelimit-reset', resetTime.toString())

0 commit comments

Comments
 (0)