singleton: true not working - each remote bundles its own copy of shared modules #4539
Unanswered
nicatabdullay3v
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a host app with multiple remotes using @module-federation/vite. Despite configuring singleton: true for all shared modules, each remote bundles its own full copy of react-oidc-context instead of using the host's version.
When I inspect the react-oidc-context chunk from any remote in Network tab, it contains the full library code (not a stub):
javascript// This is inside remote's bundle - should be a stub!
var AuthContext = React.createContext(void 0);
var AuthProvider = (props) => { ... full implementation ... }
This means useAuth() returns undefined in remotes because each remote creates its own AuthContext which has no AuthProvider.
Strange behavior
I have TWO host apps - admin-portal and web-portal - with identical configs using the same remotes. web-portal works perfectly (one copy of react-oidc-context), admin-portal breaks (multiple copies).
Config (same in all apps)
shared: {
'react-oidc-context': { singleton: true },
'oidc-client-ts': { singleton: true },
'react': { singleton: true },
'react-dom': { singleton: true },
'react-router-dom': { singleton: true },
'react-redux': { singleton: true },
'@reduxjs/toolkit': { singleton: true },
}
Version
@module-federation/vite
:^1.12.3` (latest)Expected behavior
Remotes should load a small stub that references the host's singleton
Actual behavior
Remotes load full library and create separate React contexts
Error messages
Error 1 (most common):
TypeError: Cannot read properties of undefined (reading 'user')
at usePermissions (http://localhost:4006/assets/constants.js:81:28)
at ProtectedComponent (http://localhost:4006/assets/constants.js:99:49)
at Nh (http://localhost:4007/assets/index.js:215:137)
Beta Was this translation helpful? Give feedback.
All reactions