Skip to content

Commit d5a3072

Browse files
eatlaksonEthan AtlaksonScriptedAlchemy
authored
feat(sdk): enable trusted type functionality (#2974)
Co-authored-by: Ethan Atlakson <[email protected]> Co-authored-by: Zack Jackson <[email protected]>
1 parent ea6d417 commit d5a3072

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.changeset/mean-geckos-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/sdk': patch
3+
---
4+
5+
dont set script.src if it is set by createScriptHook

packages/sdk/__tests__/dom.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ describe('createScript', () => {
126126

127127
it('should set section attributes on the script element', () => {
128128
const url = 'https://example.com/script.js';
129+
const scriptHookUrl = 'https://example.com/hook-script.js';
129130
const cb = jest.fn();
130131
const attrs = {
131132
async: true,
@@ -138,14 +139,15 @@ describe('createScript', () => {
138139
attrs,
139140
createScriptHook: (url) => {
140141
const scriptEle = document.createElement('script');
141-
scriptEle.src = url;
142+
scriptEle.src = scriptHookUrl;
142143
scriptEle.crossOrigin = 'use-credentials';
143144
scriptEle.async = false;
144145
return scriptEle;
145146
},
146147
});
147148

148149
// if user return element by createScriptHook, it will not add default attrs
150+
expect(script.src).toBe(scriptHookUrl);
149151
expect(script.async).toBe(false);
150152
expect(script.crossOrigin).toBe('use-credentials');
151153
expect(script.getAttribute('data-test')).toBe(null);
@@ -223,7 +225,7 @@ describe('createLink', () => {
223225
},
224226
});
225227

226-
// if user return element by createScriptHook, it will not add default attrs
228+
// if user return element by createLinkHook, it will not add default attrs
227229
expect(link.rel).toBe('');
228230
expect(link.crossOrigin).toBe('use-credentials');
229231
expect(link.getAttribute('as')).toBe(null);

packages/sdk/src/dom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export function createScript(info: {
5050
const attrs = info.attrs;
5151
script = document.createElement('script');
5252
script.type = attrs?.['type'] === 'module' ? 'module' : 'text/javascript';
53-
script.src = info.url;
5453
let createScriptRes: CreateScriptHookReturnDom = undefined;
5554
if (info.createScriptHook) {
5655
createScriptRes = info.createScriptHook(info.url, info.attrs);
@@ -66,6 +65,9 @@ export function createScript(info: {
6665
}
6766
}
6867
}
68+
if (!script.src) {
69+
script.src = info.url;
70+
}
6971
if (attrs && !createScriptRes) {
7072
Object.keys(attrs).forEach((name) => {
7173
if (script) {

0 commit comments

Comments
 (0)