Skip to content

Fix generating same ids when using different apps, e.g. in module federation#1917

Merged
matyasf merged 1 commit intomasterfrom
module_fed_id_fix
Mar 31, 2025
Merged

Fix generating same ids when using different apps, e.g. in module federation#1917
matyasf merged 1 commit intomasterfrom
module_fed_id_fix

Conversation

@matyasf
Copy link
Collaborator

@matyasf matyasf commented Mar 26, 2025

The cause of this issue was that we generate IDs on the ESM module level, and if 2 or more InstUI applications are executed as a downloaded .js file (like in Module federation) the instance counters are not aware of each other and are generating the same IDs causing all kinds of issues, e.g. with Radio buttons.

To test: Check out https://github.com/matyasf/module-federation-instui and use this build as a dependency -- version 10.14.1-pr-snapshot-1743089682418

  • Radiobuttons should work if both the host and the guest app use this code.
  • Radiobuttons should work if one of the apps uses this code, the other one an earlier build (e.g. InstUI v9
  • Read the updated docs

@matyasf matyasf self-assigned this Mar 26, 2025
@github-actions
Copy link

github-actions bot commented Mar 26, 2025

PR Preview Action v1.6.0
Preview removed because the pull request was closed.
2025-03-31 14:11 UTC

@matyasf matyasf changed the title WIP fix(ui-top-nav-bar,ui-react-utils): fix having the same ids in module federation apps Fix generating same ids in module federation apps causing all kind of issues Mar 27, 2025
Comment on lines +39 to +46
function generateInstanceCounterMap(): DeterministicIdProviderValue {
if (globalThis[__INSTUI_GLOBAL_INSTANCE_COUNTER__]) {
return globalThis[__INSTUI_GLOBAL_INSTANCE_COUNTER__]
}
const map = new Map<string, number>()
globalThis[__INSTUI_GLOBAL_INSTANCE_COUNTER__] = map
return map
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main fix

const { currentPageId, renderOptionContent } = options

const customPopoverIdMap = generateInstanceCounterMap()
const customPopoverIdMap = new Map<string, number>()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the global counter caused here a weird bug, I think because it was re-rendered multiple times the IDs got out of sync causing the menu buttons in SmallViewport mode to break.

@matyasf matyasf requested review from HerrTopi and balzss March 27, 2025 10:51
@matyasf matyasf changed the title Fix generating same ids in module federation apps causing all kind of issues Fix generating same ids when using different apps, e.g. in module federation Mar 27, 2025
Copy link
Contributor

@balzss balzss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rewriting a lot of the regression test in this commit so you changes would be deleted (or updated) anyway, can you leave it out from this pr?

var __INSTUI_GLOBAL_INSTANCE_COUNTER__: Map<string, number>
}
const __INSTUI_GLOBAL_INSTANCE_COUNTER__ = '__INSTUI_GLOBAL_INSTANCE_COUNTER__'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is the same as the above, global name on the globalThis. Works but could be more readable if you'd rename the const variable to something else

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I've renamed it

@matyasf matyasf force-pushed the module_fed_id_fix branch from d05e0c4 to 7a5670d Compare March 31, 2025 13:53
…s of InstUI, e.g. module federation

Its fixed by moving the instance counter to the global object. Also the generation pattern is
altered slightly to work nicely with older versions of InstUI which do not use this new global
object

Fixes INSTUI-4484
@matyasf matyasf force-pushed the module_fed_id_fix branch from 7a5670d to 0eb8ff5 Compare March 31, 2025 13:57
@matyasf matyasf requested a review from balzss March 31, 2025 14:05
@matyasf matyasf merged commit a0bb4d0 into master Mar 31, 2025
11 checks passed
@matyasf matyasf deleted the module_fed_id_fix branch March 31, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants