diff --git a/.changeset/tall-stingrays-approve.md b/.changeset/tall-stingrays-approve.md new file mode 100644 index 000000000..344420ec9 --- /dev/null +++ b/.changeset/tall-stingrays-approve.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': patch +--- + +Refactor get cdn to have smaller footprint diff --git a/packages/browser/src/browser/__tests__/standalone-analytics.test.ts b/packages/browser/src/browser/__tests__/standalone-analytics.test.ts index f318a2abe..d2e6c113e 100644 --- a/packages/browser/src/browser/__tests__/standalone-analytics.test.ts +++ b/packages/browser/src/browser/__tests__/standalone-analytics.test.ts @@ -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' @@ -127,7 +127,7 @@ describe('standalone bundle', () => { // @ts-ignore ignore Response required fields .mockImplementation((): Promise => fetchSettings) - await loadLegacySettings(segmentDotCom) + await install() expect(unfetch).toHaveBeenCalledWith( 'https://cdn.foo.com/v1/projects/foo/settings' @@ -142,7 +142,7 @@ describe('standalone bundle', () => { const mockCdn = 'http://my-overridden-cdn.com' window.analytics._cdn = mockCdn - await loadLegacySettings(segmentDotCom) + await install() expect(unfetch).toHaveBeenCalledWith(expect.stringContaining(mockCdn)) }) diff --git a/packages/browser/src/browser/index.ts b/packages/browser/src/browser/index.ts index b8fe6e078..938938b35 100644 --- a/packages/browser/src/browser/index.ts +++ b/packages/browser/src/browser/index.ts @@ -79,11 +79,9 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings { export function loadLegacySettings( writeKey: string, - cdnURL?: string + cdnURL: string ): Promise { - 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) => { @@ -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