Skip to content

Commit e8e0969

Browse files
authored
fix(sdk): the returned properties are used by default when create script and create link hook (#2608)
1 parent 652c8a2 commit e8e0969

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

.changeset/strange-bottles-confess.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+
fix: The returned properties are used by default when create script and create link hook

packages/sdk/__tests__/dom.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ describe('createScript', () => {
123123

124124
expect(clearTimeout).toHaveBeenCalled();
125125
});
126+
127+
it('should set section attributes on the script element', () => {
128+
const url = 'https://example.com/script.js';
129+
const cb = jest.fn();
130+
const attrs = {
131+
async: true,
132+
'data-test': 'test',
133+
crossOrigin: 'anonymous',
134+
};
135+
const { script } = createScript({
136+
url,
137+
cb,
138+
attrs,
139+
createScriptHook: (url) => {
140+
const scriptEle = document.createElement('script');
141+
scriptEle.src = url;
142+
scriptEle.crossOrigin = 'use-credentials';
143+
scriptEle.async = false;
144+
return scriptEle;
145+
},
146+
});
147+
148+
expect(script.async).toBe(true);
149+
expect(script.crossOrigin).toBe('use-credentials');
150+
expect(script.getAttribute('data-test')).toBe('test');
151+
});
126152
});
127153
});
128154

@@ -175,6 +201,33 @@ describe('createLink', () => {
175201
expect(link.getAttribute('data-test')).toBe('test');
176202
});
177203

204+
it('should set section attributes on the link element', () => {
205+
const url = 'https://example.com/script.js';
206+
const cb = jest.fn();
207+
const attrs = {
208+
rel: 'preload',
209+
as: 'script',
210+
'data-test': 'test',
211+
crossOrigin: 'anonymous',
212+
};
213+
const { link } = createLink({
214+
url,
215+
cb,
216+
attrs,
217+
createLinkHook: (url) => {
218+
const linkEle = document.createElement('link');
219+
linkEle.href = url;
220+
linkEle.crossOrigin = 'use-credentials';
221+
return linkEle;
222+
},
223+
});
224+
225+
expect(link.rel).toBe('preload');
226+
expect(link.crossOrigin).toBe('use-credentials');
227+
expect(link.getAttribute('as')).toBe('script');
228+
expect(link.getAttribute('data-test')).toBe('test');
229+
});
230+
178231
it('should call the callback when the link loads', () => {
179232
const url = 'https://example.com/script.js';
180233
const cb = jest.fn();

packages/sdk/src/dom.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export function createScript(info: {
7171
if (script) {
7272
if (name === 'async' || name === 'defer') {
7373
script[name] = attrs[name];
74-
} else {
74+
// Attributes that do not exist are considered overridden
75+
} else if (!script.getAttribute(name)) {
7576
script.setAttribute(name, attrs[name]);
7677
}
7778
}
@@ -160,7 +161,7 @@ export function createLink(info: {
160161
const attrs = info.attrs;
161162
if (attrs) {
162163
Object.keys(attrs).forEach((name) => {
163-
if (link) {
164+
if (link && !link.getAttribute(name)) {
164165
link.setAttribute(name, attrs[name]);
165166
}
166167
});

0 commit comments

Comments
 (0)