Skip to content

Commit

Permalink
Fix staging-only bug where integrations URL would not respect CDN UR…
Browse files Browse the repository at this point in the history
…L overrides (#800)
  • Loading branch information
silesky authored Feb 24, 2023
1 parent 830139b commit fe98d5e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-jokes-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fix staging-only bug where integrations URL would not respect CDN URL overrides
25 changes: 25 additions & 0 deletions packages/browser/src/plugins/remote-loader/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
13 changes: 4 additions & 9 deletions packages/browser/src/plugins/remote-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit fe98d5e

Please sign in to comment.