Skip to content

Commit

Permalink
CDN URL on Analytics Settings (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg authored Sep 25, 2024
1 parent 9182880 commit 3f58366
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-items-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Refactoring CDN Url to be available from analytics settings
1 change: 1 addition & 0 deletions packages/browser/src/browser/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ describe('public settings api', () => {
writeKey,
cdnSettings: cdnSettingsMinimal,
timeout: 300,
cdnURL: 'https://cdn.segment.com',
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ describe('standalone bundle', () => {
// @ts-ignore ignore Response required fields
.mockImplementation((): Promise<Response> => fetchSettings)

await loadCDNSettings(segmentDotCom)
const mockCdn = 'https://cdn.foo.com'

await loadCDNSettings(segmentDotCom, mockCdn)

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

getGlobalAnalytics()!._cdn = mockCdn
await loadCDNSettings(segmentDotCom)
await loadCDNSettings(segmentDotCom, mockCdn)

expect(unfetch).toHaveBeenCalledWith(expect.stringContaining(mockCdn))
})
Expand Down
10 changes: 4 additions & 6 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ export interface AnalyticsBrowserSettings {

export function loadCDNSettings(
writeKey: string,
cdnURL?: string
baseUrl: string
): Promise<CDNSettings> {
const baseUrl = cdnURL ?? getCDN()

return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
.then((res) => {
if (!res.ok) {
Expand Down Expand Up @@ -355,9 +353,9 @@ async function loadAnalytics(
preInitBuffer.add(new PreInitMethodCall('page', []))
}

const cdnURL = settings.cdnURL ?? getCDN()
let cdnSettings =
settings.cdnSettings ??
(await loadCDNSettings(settings.writeKey, settings.cdnURL))
settings.cdnSettings ?? (await loadCDNSettings(settings.writeKey, cdnURL))

if (options.updateCDNSettings) {
cdnSettings = options.updateCDNSettings(cdnSettings)
Expand All @@ -379,7 +377,7 @@ async function loadAnalytics(
...options,
}

const analytics = new Analytics({ ...settings, cdnSettings }, options)
const analytics = new Analytics({ ...settings, cdnSettings, cdnURL }, options)

attachInspector(analytics)

Expand Down
5 changes: 4 additions & 1 deletion packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ export class AnalyticsInstanceSettings {
* This is an unstable API, it may change in the future without warning.
*/
readonly cdnSettings: CDNSettings
readonly cdnURL?: string

/**
* Auto-track specific timeout setting for legacy purposes.
* Auto-track specific timeout setting for legacy purposes.
*/
timeout = 300

Expand All @@ -92,6 +93,7 @@ export class AnalyticsInstanceSettings {
integrations: {},
edgeFunction: {},
}
this.cdnURL = settings.cdnURL
}
}

Expand All @@ -101,6 +103,7 @@ export class AnalyticsInstanceSettings {
export interface AnalyticsSettings {
writeKey: string
cdnSettings?: CDNSettings
cdnURL?: string
}

export interface InitOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Segment.io retries 500s and 429', () => {
})

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

0 comments on commit 3f58366

Please sign in to comment.