diff --git a/.changeset/eight-jokes-poke.md b/.changeset/eight-jokes-poke.md new file mode 100644 index 000000000..94eea547b --- /dev/null +++ b/.changeset/eight-jokes-poke.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-next': patch +--- + +Fix staging-only bug where integrations URL would not respect CDN URL overrides diff --git a/packages/browser/src/plugins/remote-loader/__tests__/index.test.ts b/packages/browser/src/plugins/remote-loader/__tests__/index.test.ts index 1044511ca..237cebfe9 100644 --- a/packages/browser/src/plugins/remote-loader/__tests__/index.test.ts +++ b/packages/browser/src/plugins/remote-loader/__tests__/index.test.ts @@ -89,6 +89,31 @@ describe('Remote Loader', () => { expect(loader.loadScript).toHaveBeenCalledWith('foo.com/actions/file.js') }) + it('should work if the cdn is staging', async () => { + const stagingURL = 'https://cdn.segment.build/actions/foo.js' + + window.analytics = {} + window.analytics._cdn = 'foo.com' + await remoteLoader( + { + integrations: {}, + remotePlugins: [ + { + name: 'remote plugin', + creationName: 'remote plugin', + url: stagingURL, + libraryName: 'testPlugin', + settings: {}, + }, + ], + }, + {}, + {} + ) + + expect(loader.loadScript).toHaveBeenCalledWith('foo.com/actions/foo.js') + }) + it('should attempt calling the library', async () => { await remoteLoader( { diff --git a/packages/browser/src/plugins/remote-loader/index.ts b/packages/browser/src/plugins/remote-loader/index.ts index 8fa8566e0..f3d77eee4 100644 --- a/packages/browser/src/plugins/remote-loader/index.ts +++ b/packages/browser/src/plugins/remote-loader/index.ts @@ -175,6 +175,7 @@ export async function remoteLoader( async (remotePlugin) => { if (isPluginDisabled(userIntegrations, remotePlugin)) return try { + const defaultCdn = new RegExp('https://cdn.segment.(com|build)') if (obfuscate) { const urlSplit = remotePlugin.url.split('/') const name = urlSplit[urlSplit.length - 2] @@ -183,20 +184,14 @@ export async function remoteLoader( btoa(name).replace(/=/g, '') ) try { - await loadScript( - obfuscatedURL.replace('https://cdn.segment.com', cdn) - ) + await loadScript(obfuscatedURL.replace(defaultCdn, cdn)) } catch (error) { // Due to syncing concerns it is possible that the obfuscated action destination (or requested version) might not exist. // We should use the unobfuscated version as a fallback. - await loadScript( - remotePlugin.url.replace('https://cdn.segment.com', cdn) - ) + await loadScript(remotePlugin.url.replace(defaultCdn, cdn)) } } else { - await loadScript( - remotePlugin.url.replace('https://cdn.segment.com', cdn) - ) + await loadScript(remotePlugin.url.replace(defaultCdn, cdn)) } const libraryName = remotePlugin.libraryName