Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix staging-only bug where integrations URL would not respect CDN URL overrides #800

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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