Skip to content

Commit

Permalink
refactor get cdn to have smaller footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Oct 26, 2022
1 parent 3d31bd0 commit 4cbe69c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-stingrays-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Refactor get cdn to have smaller footprint
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import jsdom, { JSDOM } from 'jsdom'
import { InitOptions } from '../../'
import { AnalyticsBrowser, loadLegacySettings } from '../../browser'
import { AnalyticsBrowser } from '../../browser'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import { install, AnalyticsStandalone } from '../standalone-analytics'
import unfetch from 'unfetch'
Expand Down Expand Up @@ -127,10 +127,10 @@ describe('standalone bundle', () => {
// @ts-ignore ignore Response required fields
.mockImplementation((): Promise<Response> => fetchSettings)

await loadLegacySettings(segmentDotCom)
await AnalyticsBrowser.standalone('my-write-key')

expect(unfetch).toHaveBeenCalledWith(
'https://cdn.foo.com/v1/projects/foo/settings'
'https://cdn.foo.com/v1/projects/my-write-key/settings'
)
})

Expand All @@ -142,7 +142,7 @@ describe('standalone bundle', () => {
const mockCdn = 'http://my-overridden-cdn.com'

window.analytics._cdn = mockCdn
await loadLegacySettings(segmentDotCom)
await AnalyticsBrowser.standalone('abc')

expect(unfetch).toHaveBeenCalledWith(expect.stringContaining(mockCdn))
})
Expand Down
8 changes: 3 additions & 5 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {

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

return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
return fetch(`${cdnURL}/v1/projects/${writeKey}/settings`)
.then((res) => {
if (!res.ok) {
return res.text().then((errorResponseMessage) => {
Expand Down Expand Up @@ -263,7 +261,7 @@ async function loadAnalytics(

const legacySettings =
settings.cdnSettings ??
(await loadLegacySettings(settings.writeKey, settings.cdnURL))
(await loadLegacySettings(settings.writeKey, settings.cdnURL || getCDN()))

const retryQueue: boolean =
legacySettings.integrations['Segment.io']?.retryQueue ?? true
Expand Down

0 comments on commit 4cbe69c

Please sign in to comment.