Skip to content

Commit 9ad70ef

Browse files
committed
feat(nf): cache read federation config
1 parent 06c9104 commit 9ad70ef

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

libs/native-federation-runtime/src/lib/model/externals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SharedInfo } from './federation-info';
2+
import { globalCache } from './global-cache';
23

3-
const externals = new Map<string, string>();
4+
const externals = globalCache.externals;
45

56
function getExternalKey(shared: SharedInfo) {
67
return `${shared.packageName}@${shared.version}`;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Remote } from './remotes';
2+
3+
export const nfNamespace = '__NATIVE_FEDERATION__';
4+
5+
export type NfCache = {
6+
externals: Map<string, string>;
7+
remoteNamesToRemote: Map<string, Remote>;
8+
baseUrlToRemoteNames: Map<string, string>;
9+
};
10+
11+
export type Global = {
12+
[nfNamespace]: NfCache;
13+
};
14+
15+
const global = globalThis as unknown as Global;
16+
17+
global[nfNamespace] ??= {
18+
externals: new Map<string, string>(),
19+
remoteNamesToRemote: new Map<string, Remote>(),
20+
baseUrlToRemoteNames: new Map<string, string>(),
21+
};
22+
23+
export const globalCache = global[nfNamespace];

libs/native-federation-runtime/src/lib/model/remotes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { FederationInfo } from './federation-info';
2+
import { globalCache } from './global-cache';
23

34
export type Remote = FederationInfo & {
45
baseUrl: string;
56
};
67

7-
const remoteNamesToRemote = new Map<string, Remote>();
8-
const baseUrlToRemoteNames = new Map<string, string>();
8+
const remoteNamesToRemote = globalCache.remoteNamesToRemote;
9+
const baseUrlToRemoteNames = globalCache.baseUrlToRemoteNames;
910

1011
export function addRemote(remoteName: string, remote: Remote): void {
1112
remoteNamesToRemote.set(remoteName, remote);

0 commit comments

Comments
 (0)