Skip to content

Commit ef5cd39

Browse files
authored
Fix actions loading (#710)
1 parent c5d2a99 commit ef5cd39

File tree

3 files changed

+191
-6
lines changed

3 files changed

+191
-6
lines changed

.changeset/breezy-kids-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Fixes an issue impacting a small number of destinations where explicitly enabling or disabling an integration on load would not work as expected.

packages/browser/src/plugins/remote-loader/__tests__/index.test.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,167 @@ describe('Remote Loader', () => {
533533
)
534534
})
535535

536+
// Action destinations should be toggled using the `integration` name which matches the `creationName`
537+
// Most action destinations have the same value for creationName and name, but a few (Amplitude) do not.
538+
// Continue to support toggling with plugin.name for backwards compatibility.
539+
it('loads destinations when `All: false` but is enabled (pluginName)', async () => {
540+
const cdnSettings = {
541+
integrations: {
542+
oldValidName: {
543+
versionSettings: {
544+
componentTypes: [],
545+
},
546+
},
547+
},
548+
remotePlugins: [
549+
{
550+
name: 'valid',
551+
creationName: 'oldValidName',
552+
url: 'cdn/path/to/file.js',
553+
libraryName: 'testPlugin',
554+
settings: {
555+
subscriptions: [],
556+
versionSettings: {
557+
componentTypes: [],
558+
},
559+
},
560+
},
561+
],
562+
}
563+
564+
await AnalyticsBrowser.load(
565+
{ writeKey: '', cdnSettings },
566+
{
567+
integrations: {
568+
All: false,
569+
valid: true,
570+
},
571+
}
572+
)
573+
574+
expect(pluginFactory).toHaveBeenCalledTimes(1)
575+
expect(pluginFactory).toHaveBeenCalledWith(
576+
expect.objectContaining(cdnSettings.remotePlugins[0].settings)
577+
)
578+
})
579+
580+
it('loads destinations when `All: false` but is enabled (creationName)', async () => {
581+
const cdnSettings = {
582+
integrations: {
583+
oldValidName: {
584+
versionSettings: {
585+
componentTypes: [],
586+
},
587+
},
588+
},
589+
remotePlugins: [
590+
{
591+
name: 'valid',
592+
creationName: 'oldValidName',
593+
url: 'cdn/path/to/file.js',
594+
libraryName: 'testPlugin',
595+
settings: {
596+
subscriptions: [],
597+
versionSettings: {
598+
componentTypes: [],
599+
},
600+
},
601+
},
602+
],
603+
}
604+
605+
await AnalyticsBrowser.load(
606+
{ writeKey: '', cdnSettings },
607+
{
608+
integrations: {
609+
All: false,
610+
oldValidName: true,
611+
},
612+
}
613+
)
614+
615+
expect(pluginFactory).toHaveBeenCalledTimes(1)
616+
expect(pluginFactory).toHaveBeenCalledWith(
617+
expect.objectContaining(cdnSettings.remotePlugins[0].settings)
618+
)
619+
})
620+
621+
it('does not load destinations when disabled via pluginName', async () => {
622+
const cdnSettings = {
623+
integrations: {
624+
oldValidName: {
625+
versionSettings: {
626+
componentTypes: [],
627+
},
628+
},
629+
},
630+
remotePlugins: [
631+
{
632+
name: 'valid',
633+
creationName: 'oldValidName',
634+
url: 'cdn/path/to/file.js',
635+
libraryName: 'testPlugin',
636+
settings: {
637+
subscriptions: [],
638+
versionSettings: {
639+
componentTypes: [],
640+
},
641+
},
642+
},
643+
],
644+
}
645+
646+
await AnalyticsBrowser.load(
647+
{ writeKey: '', cdnSettings },
648+
{
649+
integrations: {
650+
All: true,
651+
valid: false,
652+
},
653+
}
654+
)
655+
656+
expect(pluginFactory).toHaveBeenCalledTimes(0)
657+
})
658+
659+
it('does not load destinations when disabled via creationName', async () => {
660+
const cdnSettings = {
661+
integrations: {
662+
oldValidName: {
663+
versionSettings: {
664+
componentTypes: [],
665+
},
666+
},
667+
},
668+
remotePlugins: [
669+
{
670+
name: 'valid',
671+
creationName: 'oldValidName',
672+
url: 'cdn/path/to/file.js',
673+
libraryName: 'testPlugin',
674+
settings: {
675+
subscriptions: [],
676+
versionSettings: {
677+
componentTypes: [],
678+
},
679+
},
680+
},
681+
],
682+
}
683+
684+
await AnalyticsBrowser.load(
685+
{ writeKey: '', cdnSettings },
686+
{
687+
integrations: {
688+
All: true,
689+
oldValidName: false,
690+
},
691+
}
692+
)
693+
694+
expect(pluginFactory).toHaveBeenCalledTimes(0)
695+
})
696+
536697
it('applies remote routing rules based on creation name', async () => {
537698
const validPlugin = {
538699
name: 'valid',

packages/browser/src/plugins/remote-loader/index.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ function validate(pluginLike: unknown): pluginLike is Plugin[] {
135135
return true
136136
}
137137

138+
function isPluginDisabled(
139+
userIntegrations: Integrations,
140+
remotePlugin: RemotePlugin
141+
) {
142+
const creationNameEnabled = userIntegrations[remotePlugin.creationName]
143+
const currentNameEnabled = userIntegrations[remotePlugin.name]
144+
145+
// Check that the plugin isn't explicitly enabled when All: false
146+
if (
147+
userIntegrations.All === false &&
148+
!creationNameEnabled &&
149+
!currentNameEnabled
150+
) {
151+
return true
152+
}
153+
154+
// Check that the plugin isn't explicitly disabled
155+
if (creationNameEnabled === false || currentNameEnabled === false) {
156+
return true
157+
}
158+
159+
return false
160+
}
161+
138162
export async function remoteLoader(
139163
settings: LegacySettings,
140164
userIntegrations: Integrations,
@@ -149,12 +173,7 @@ export async function remoteLoader(
149173

150174
const pluginPromises = (settings.remotePlugins ?? []).map(
151175
async (remotePlugin) => {
152-
if (
153-
(userIntegrations.All === false &&
154-
!userIntegrations[remotePlugin.name]) ||
155-
userIntegrations[remotePlugin.name] === false
156-
)
157-
return
176+
if (isPluginDisabled(userIntegrations, remotePlugin)) return
158177
try {
159178
if (obfuscate) {
160179
const urlSplit = remotePlugin.url.split('/')

0 commit comments

Comments
 (0)