Skip to content

Commit f5bd3c3

Browse files
fix(sdk): issue where duplicate links were created when existing links had no rel attribute
1 parent a79ad53 commit f5bd3c3

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.changeset/ai-eager-mouse.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"@module-federation/sdk": patch
2+
---
3+
4+
Fixes an issue where duplicate links were created when existing links had no rel attribute.
5+
6+
- Added a test in `dom.spec.ts` to ensure links without `rel` attribute aren't duplicated.
7+
- Modified the `createLink` function in `dom.ts` to treat `null` and `undefined` `rel` attributes as equivalent when checking for existing links.
8+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"prepare": "husky install",
5252
"changeset": "changeset",
5353
"build:packages": "npx nx affected -t build --parallel=10 --exclude='*,!tag:type:pkg'",
54-
"changegen": "./changeset-gen.js --path ./packages/enhanced --staged && ./changeset-gen.js --path ./packages/node --staged && ./changeset-gen.js --path ./packages/runtime --staged && ./changeset-gen.js --path ./packages/data-prefetch --staged && ./changeset-gen.js --path ./packages/nextjs-mf --staged && ./changeset-gen.js --path ./packages/dts-plugin --staged",
54+
"changegen": "./changeset-gen.js --path ./packages/enhanced --staged && ./changeset-gen.js --path ./packages/node --staged && ./changeset-gen.js --path ./packages/runtime --staged && ./changeset-gen.js --path ./packages/data-prefetch --staged && ./changeset-gen.js --path ./packages/nextjs-mf --staged && ./changeset-gen.js --path ./packages/dts-plugin --staged && ./changeset-gen.js --path ./packages/sdk --staged",
5555
"commitgen:staged": "./commit-gen.js --path ./packages --staged",
5656
"commitgen:main": "./commit-gen.js --path ./packages",
5757
"changeset:status": "changeset status",

packages/sdk/__tests__/dom.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,25 @@ describe('createLink', () => {
283283

284284
expect(link).toBe(customLink);
285285
});
286+
287+
it('should not create a duplicate link when existing link has no rel and attrs.rel is undefined (https://github.com/module-federation/core/issues/3705)', () => {
288+
// Simulate an existing link with no rel attribute
289+
const url = 'https://example.com/stylesheet.css';
290+
const existingLink = document.createElement('link');
291+
existingLink.setAttribute('href', url);
292+
// Note: no rel attribute set
293+
document.head.appendChild(existingLink);
294+
295+
// Now call createLink with matching url and no rel in attrs
296+
const cb = jest.fn();
297+
const { link, needAttach } = createLink({
298+
url,
299+
cb,
300+
attrs: { as: 'style' }, // rel is omitted, so info.attrs['rel'] is undefined
301+
});
302+
303+
// Should reuse the existing link, not create a new one
304+
expect(link).toBe(existingLink);
305+
expect(needAttach).toBe(false);
306+
});
286307
});

packages/sdk/src/dom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ export function createLink(info: {
156156
const l = links[i];
157157
const linkHref = l.getAttribute('href');
158158
const linkRel = l.getAttribute('rel');
159+
// Use == to treat null and undefined as equivalent (see https://github.com/module-federation/core/issues/3705)
159160
if (
160161
linkHref &&
161162
isStaticResourcesEqual(linkHref, info.url) &&
162-
linkRel === info.attrs['rel']
163+
linkRel == info.attrs['rel']
163164
) {
164165
link = l;
165166
needAttach = false;

0 commit comments

Comments
 (0)