Skip to content

Commit f8ae21c

Browse files
feat(dynamic-federation): add nonce support for remote entry and module loading
1 parent bc286f6 commit f8ae21c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

libs/mf-runtime/src/lib/loader/dynamic-federation.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare const __webpack_share_scopes__: { default: Scope };
2929
type ContainerMap = { [key: string]: Container };
3030

3131
const containerMap: ContainerMap = {};
32-
const remoteMap = {};
32+
const remoteMap: { [key: string]: boolean } = {};
3333

3434
let isDefaultScopeInitialized = false;
3535

@@ -70,6 +70,7 @@ export type LoadRemoteEntryScriptOptions = {
7070
type?: 'script';
7171
remoteEntry: string;
7272
remoteName: string;
73+
nonce?: string;
7374
};
7475

7576
export type LoadRemoteEntryEsmOptions = {
@@ -86,14 +87,19 @@ export async function loadRemoteEntry(
8687
): Promise<void>;
8788
export async function loadRemoteEntry(
8889
remoteEntryOrOptions: string | LoadRemoteEntryOptions,
89-
remoteName?: string
90+
remoteName?: string,
91+
nonce?: string
9092
): Promise<void> {
9193
if (typeof remoteEntryOrOptions === 'string') {
9294
const remoteEntry = remoteEntryOrOptions;
93-
return await loadRemoteScriptEntry(remoteEntry, remoteName);
95+
return await loadRemoteScriptEntry(remoteEntry, remoteName, nonce);
9496
} else if (remoteEntryOrOptions.type === 'script') {
9597
const options = remoteEntryOrOptions;
96-
return await loadRemoteScriptEntry(options.remoteEntry, options.remoteName);
98+
return await loadRemoteScriptEntry(
99+
options.remoteEntry,
100+
options.remoteName,
101+
options.nonce
102+
);
97103
} else if (remoteEntryOrOptions.type === 'module') {
98104
const options = remoteEntryOrOptions;
99105
await loadRemoteModuleEntry(options.remoteEntry);
@@ -114,7 +120,8 @@ async function loadRemoteModuleEntry(remoteEntry: string): Promise<void> {
114120

115121
async function loadRemoteScriptEntry(
116122
remoteEntry: string,
117-
remoteName: string
123+
remoteName: string,
124+
nonce?: string
118125
): Promise<void> {
119126
return new Promise<void>((resolve, reject) => {
120127
// Is remoteEntry already loaded?
@@ -125,6 +132,9 @@ async function loadRemoteScriptEntry(
125132

126133
const script = document.createElement('script');
127134
script.src = remoteEntry;
135+
if (nonce) {
136+
script.setAttribute('nonce', nonce);
137+
}
128138

129139
script.onerror = reject;
130140

@@ -149,6 +159,7 @@ export type LoadRemoteModuleScriptOptions = {
149159
remoteEntry?: string;
150160
remoteName: string;
151161
exposedModule: string;
162+
nonce?: string;
152163
};
153164

154165
export type LoadRemoteModuleEsmOptions = {
@@ -218,6 +229,7 @@ export async function loadRemoteModule<T = any>(
218229
type: 'script',
219230
remoteEntry: options.remoteEntry,
220231
remoteName: options.remoteName,
232+
nonce: options.nonce,
221233
};
222234
key = options.remoteName;
223235
} else if (options.type === 'module') {

0 commit comments

Comments
 (0)