Skip to content

Commit a2bfb9b

Browse files
authored
fix: fixed preload resource reuse issues and devtool hmr shared (#2580)
1 parent 129762d commit a2bfb9b

File tree

11 files changed

+230
-128
lines changed

11 files changed

+230
-128
lines changed

.changeset/perfect-camels-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/devtools': patch
3+
---
4+
5+
fix: devtools Adjustments hmr changes the behavior of shared to make changes more stable

.changeset/wet-geckos-tan.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'runtime-remote1': patch
3+
'@module-federation/runtime': patch
4+
'@module-federation/sdk': patch
5+
---
6+
7+
fix: In load remote, link preload is not used to preload resources, preventing resource reloading

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
run: npx nx affected -t lint --parallel=7 --exclude='*,!tag:type:pkg'
4545

4646
- name: Run Affected Test
47-
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:type:pkg'
47+
run: npx nx affected -t test --parallel=3 --exclude='*,!tag:type:pkg' --skip-cache
4848

4949
# - name: E2E Test for Next.js Dev
5050
# run: |

apps/runtime-demo/3006-runtime-remote/src/components/WebpackPng.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import png from '../../public/webpack.png';
2+
import './a.css';
23

34
export default function WebpackPng() {
45
return <img className="remote1-webpack-png" src={png} alt="webpack png" />;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
span {
2+
color: red;
3+
}

packages/chrome-devtools/src/utils/chrome/fast-refresh.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { __FEDERATION_DEVTOOLS__ } from '../../template';
1111
const fastRefreshPlugin = (): FederationRuntimePlugin => {
1212
return {
1313
name: 'mf-fast-refresh-plugin',
14-
// @ts-expect-error
15-
beforeInit({ origin, userOptions, options, shareInfo }) {
14+
beforeInit({ userOptions, ...args }) {
15+
const shareInfo = userOptions.shared;
1616
let enableFastRefresh: boolean;
1717
let devtoolsMessage;
1818

@@ -26,7 +26,7 @@ const fastRefreshPlugin = (): FederationRuntimePlugin => {
2626
}
2727
}
2828

29-
if (isObject(shareInfo)) {
29+
if (shareInfo && isObject(shareInfo)) {
3030
let orderResolve: (value?: unknown) => void;
3131
const orderPromise = new Promise((resolve) => {
3232
orderResolve = resolve;
@@ -83,16 +83,13 @@ const fastRefreshPlugin = (): FederationRuntimePlugin => {
8383
});
8484

8585
return {
86-
origin,
8786
userOptions,
88-
options,
89-
shareInfo,
87+
...args,
9088
};
9189
} else {
9290
return {
93-
origin,
9491
userOptions,
95-
options,
92+
...args,
9693
};
9794
}
9895
},

packages/runtime/__tests__/__snapshots__/preload-remote.spec.ts.snap

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ exports[`preload-remote inBrowser > 1 preload with default config 1`] = `
44
{
55
"links": [
66
{
7-
"href": "http://localhost:1111/resources/preload/preload-resource/button.sync.js",
7+
"href": "http://localhost:1111/resources/preload/preload-resource/button.sync.css",
88
"rel": "preload",
9-
"type": "script",
9+
"type": "style",
1010
},
1111
{
12-
"href": "http://localhost:1111/resources/preload/preload-resource/sub1-button/button.sync.js",
12+
"href": "http://localhost:1111/resources/preload/preload-resource/button.sync.js",
1313
"rel": "preload",
1414
"type": "script",
1515
},
1616
{
17-
"href": "http://localhost:1111/resources/preload/preload-resource/button.sync.css",
17+
"href": "http://localhost:1111/resources/preload/preload-resource/sub1-button/button.sync.js",
1818
"rel": "preload",
19-
"type": "style",
19+
"type": "script",
2020
},
2121
],
2222
"scripts": [
2323
{
24-
"crossorigin": "",
24+
"crossorigin": "anonymous",
2525
"src": "http://localhost:1111/resources/preload/preload-resource/federation-remote-entry.js",
2626
},
2727
{
28-
"crossorigin": "",
28+
"crossorigin": "anonymous",
2929
"src": "http://localhost:1111/resources/preload/preload-resource/sub1-button/federation-remote-entry.js",
3030
},
3131
],
@@ -35,6 +35,16 @@ exports[`preload-remote inBrowser > 1 preload with default config 1`] = `
3535
exports[`preload-remote inBrowser > 2 preload with all config 1`] = `
3636
{
3737
"links": [
38+
{
39+
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.async.css",
40+
"rel": "preload",
41+
"type": "style",
42+
},
43+
{
44+
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.sync.css",
45+
"rel": "preload",
46+
"type": "style",
47+
},
3848
{
3949
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.async.js",
4050
"rel": "preload",
@@ -65,24 +75,14 @@ exports[`preload-remote inBrowser > 2 preload with all config 1`] = `
6575
"rel": "preload",
6676
"type": "script",
6777
},
68-
{
69-
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.async.css",
70-
"rel": "preload",
71-
"type": "style",
72-
},
73-
{
74-
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.sync.css",
75-
"rel": "preload",
76-
"type": "style",
77-
},
7878
],
7979
"scripts": [
8080
{
81-
"crossorigin": "",
81+
"crossorigin": "anonymous",
8282
"src": "http://localhost:1111/resources/preload/preload-resource/sub2/federation-remote-entry.js",
8383
},
8484
{
85-
"crossorigin": "",
85+
"crossorigin": "anonymous",
8686
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-button/federation-remote-entry.js",
8787
},
8888
],
@@ -100,7 +100,7 @@ exports[`preload-remote inBrowser > 3 preload with expose config 1`] = `
100100
],
101101
"scripts": [
102102
{
103-
"crossorigin": "",
103+
"crossorigin": "anonymous",
104104
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/federation-remote-entry.js",
105105
},
106106
],
@@ -118,7 +118,7 @@ exports[`preload-remote inBrowser > 3 preload with expose config 2`] = `
118118
],
119119
"scripts": [
120120
{
121-
"crossorigin": "",
121+
"crossorigin": "anonymous",
122122
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/federation-remote-entry.js",
123123
},
124124
],

packages/runtime/src/plugins/snapshot/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function snapshotPlugin(): FederationRuntimePlugin {
6767
);
6868

6969
if (assets) {
70-
preloadAssets(remoteInfo, origin, assets);
70+
preloadAssets(remoteInfo, origin, assets, false);
7171
}
7272

7373
return {

packages/runtime/src/utils/preload.ts

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createLink } from '@module-federation/sdk';
1+
import { createLink, createScript } from '@module-federation/sdk';
22
import {
33
PreloadAssets,
44
PreloadConfig,
@@ -69,6 +69,8 @@ export function preloadAssets(
6969
remoteInfo: RemoteInfo,
7070
host: FederationHost,
7171
assets: PreloadAssets,
72+
// It is used to distinguish preload from load remote parallel loading
73+
useLinkPreload: boolean = true,
7274
): void {
7375
const { cssAssets, jsAssetsWithoutEntry, entryAssets } = assets;
7476

@@ -131,52 +133,96 @@ export function preloadAssets(
131133
}
132134
});
133135

134-
const fragment = document.createDocumentFragment();
135-
cssAssets.forEach((cssUrl) => {
136-
const { link: cssEl, needAttach } = createLink(
137-
cssUrl,
138-
() => {},
139-
{
140-
rel: 'preload',
141-
as: 'style',
142-
},
143-
(url: string) => {
144-
const res = host.loaderHook.lifecycle.createLink.emit({
145-
url,
146-
});
147-
if (res instanceof HTMLLinkElement) {
148-
return res;
149-
}
150-
return;
151-
},
152-
);
153-
154-
needAttach && fragment.appendChild(cssEl);
155-
});
136+
if (useLinkPreload) {
137+
cssAssets.forEach((cssUrl) => {
138+
const { link: cssEl, needAttach } = createLink({
139+
url: cssUrl,
140+
cb: () => {},
141+
attrs: {
142+
rel: 'preload',
143+
as: 'style',
144+
crossorigin: 'anonymous',
145+
},
146+
createLinkHook: (url: string) => {
147+
const res = host.loaderHook.lifecycle.createLink.emit({
148+
url,
149+
});
150+
if (res instanceof HTMLLinkElement) {
151+
return res;
152+
}
153+
return;
154+
},
155+
});
156156

157-
jsAssetsWithoutEntry.forEach((jsUrl) => {
158-
const { link: linkEl, needAttach } = createLink(
159-
jsUrl,
160-
() => {
161-
// noop
162-
},
163-
{
164-
rel: 'preload',
165-
as: 'script',
166-
},
167-
(url: string) => {
168-
const res = host.loaderHook.lifecycle.createLink.emit({
169-
url,
170-
});
171-
if (res instanceof HTMLLinkElement) {
172-
return res;
173-
}
174-
return;
175-
},
176-
);
177-
needAttach && document.head.appendChild(linkEl);
178-
});
157+
needAttach && document.head.appendChild(cssEl);
158+
});
159+
} else {
160+
cssAssets.forEach((cssUrl) => {
161+
const { link: cssEl, needAttach } = createLink({
162+
url: cssUrl,
163+
cb: () => {},
164+
attrs: {
165+
rel: 'stylesheet',
166+
type: 'text/css',
167+
},
168+
createLinkHook: (url: string) => {
169+
const res = host.loaderHook.lifecycle.createLink.emit({
170+
url,
171+
});
172+
if (res instanceof HTMLLinkElement) {
173+
return res;
174+
}
175+
return;
176+
},
177+
});
178+
179+
needAttach && document.head.appendChild(cssEl);
180+
});
181+
}
179182

180-
document.head.appendChild(fragment);
183+
if (useLinkPreload) {
184+
jsAssetsWithoutEntry.forEach((jsUrl) => {
185+
const { link: linkEl, needAttach } = createLink({
186+
url: jsUrl,
187+
cb: () => {},
188+
attrs: {
189+
rel: 'preload',
190+
as: 'script',
191+
crossorigin: 'anonymous',
192+
},
193+
createLinkHook: (url: string) => {
194+
const res = host.loaderHook.lifecycle.createLink.emit({
195+
url,
196+
});
197+
if (res instanceof HTMLLinkElement) {
198+
return res;
199+
}
200+
return;
201+
},
202+
});
203+
needAttach && document.head.appendChild(linkEl);
204+
});
205+
} else {
206+
jsAssetsWithoutEntry.forEach((jsUrl) => {
207+
const { script: scriptEl, needAttach } = createScript({
208+
url: jsUrl,
209+
cb: () => {},
210+
attrs: {
211+
crossorigin: 'anonymous',
212+
fetchpriority: 'high',
213+
},
214+
createScriptHook: (url: string) => {
215+
const res = host.loaderHook.lifecycle.createScript.emit({
216+
url,
217+
});
218+
if (res instanceof HTMLScriptElement) {
219+
return res;
220+
}
221+
return;
222+
},
223+
});
224+
needAttach && document.head.appendChild(scriptEl);
225+
});
226+
}
181227
}
182228
}

0 commit comments

Comments
 (0)