Skip to content
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/tidy-pandas-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@module-federation/runtime-core": patch
---

Cache factories returned by synchronous shared getters for subsequent loads.
25 changes: 25 additions & 0 deletions packages/runtime-core/__tests__/instance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ describe('ModuleFederation', () => {
});
});

it('caches shared factories returned by synchronous getters', () => {
const factory = () => ({ value: 'shared' });
const getShared = rs.fn(() => factory);
const federation = new ModuleFederation({
name: '@federation/sync-shared-cache',
remotes: [],
shared: {
'sync-shared': {
version: '1.0.0',
get: getShared,
},
},
});

const firstFactory = federation.loadShareSync('sync-shared');
const secondFactory = federation.loadShareSync('sync-shared');

expect(firstFactory).toBe(factory);
expect(secondFactory).toBe(factory);
expect(getShared).toHaveBeenCalledTimes(1);
expect(federation.shareScopeMap.default['sync-shared']['1.0.0'].lib).toBe(
factory,
);
});

it('deduplicates concurrent remote module init', async () => {
let beforeInitContainerCalls = 0;
let initContainerCalls = 0;
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ export class SharedHandler {
merge(targetShared, 'loaded', loaded);
merge(targetShared, 'loading', loading);
merge(targetShared, 'get', get);
merge(targetShared, 'lib', lib);
};
scopes.forEach((sc) => {
if (!this.shareScopeMap[sc]) {
Expand Down
Loading