Skip to content

Commit d3f4639

Browse files
committed
fix(runtime): preload filter loaded resources
1 parent b88ad60 commit d3f4639

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

packages/modernjs/src/runtime/createRemoteSSRComponent.tsx

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,40 @@ export function collectSSRAssets(options: IProps) {
131131
}
132132
const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
133133
if (injectLink) {
134-
[...targetModule.assets.css.async].sort().forEach((file, index) => {
135-
links.push(
136-
<link
134+
[...targetModule.assets.css.sync, ...targetModule.assets.css.async]
135+
.sort()
136+
.forEach((file, index) => {
137+
links.push(
138+
<link
139+
key={`${file.split('.')[0]}_${index}`}
140+
href={`${publicPath}${file}`}
141+
rel="stylesheet"
142+
type="text/css"
143+
/>,
144+
);
145+
});
146+
}
147+
148+
if (injectScript) {
149+
scripts.push(
150+
<script
151+
async={true}
152+
key={remoteEntry.split('.')[0]}
153+
src={`${publicPath}${remoteEntry}`}
154+
crossOrigin="anonymous"
155+
/>,
156+
);
157+
[...targetModule.assets.js.sync].sort().forEach((file, index) => {
158+
scripts.push(
159+
<script
137160
key={`${file.split('.')[0]}_${index}`}
138-
href={`${publicPath}${file}`}
139-
rel="stylesheet"
140-
type="text/css"
161+
async={true}
162+
src={`${publicPath}${file}`}
163+
crossOrigin="anonymous"
141164
/>,
142165
);
143166
});
144167
}
145-
scripts.push(
146-
<script
147-
async={true}
148-
key={remoteEntry.split('.')[0]}
149-
src={`${publicPath}${remoteEntry}`}
150-
crossOrigin="anonymous"
151-
/>,
152-
);
153-
// if (injectScript) {
154-
// [...targetModule.assets.js.sync].sort().forEach((file, index) => {
155-
// scripts.push(
156-
// <script
157-
// key={`${file.split('.')[0]}_${index}`}
158-
// async={true}
159-
// src={`${publicPath}${file}`}
160-
// crossOrigin="anonymous"
161-
// />,
162-
// );
163-
// });
164-
// }
165168

166169
return [...scripts, ...links];
167170
}

packages/runtime-core/src/plugins/generate-preload-assets.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ProviderModuleInfo,
55
isManifestProvider,
66
getResourceUrl,
7+
isBrowserEnv,
78
} from '@module-federation/sdk';
89
import {
910
EntryAssets,
@@ -101,6 +102,14 @@ function traverseModuleInfo(
101102
}
102103
}
103104

105+
const filterExistedAssets = (type: 'link' | 'script', url: string) => {
106+
return Boolean(
107+
document.querySelector(
108+
`${type}[${type === 'link' ? 'href' : 'src'}="${url}"]`,
109+
),
110+
);
111+
};
112+
104113
// eslint-disable-next-line max-lines-per-function
105114
export function generatePreloadAssets(
106115
origin: FederationHost,
@@ -290,16 +299,20 @@ export function generatePreloadAssets(
290299
}
291300

292301
const needPreloadJsAssets = jsAssets.filter(
293-
(asset) => !loadedSharedJsAssets.has(asset),
302+
(asset) =>
303+
!loadedSharedJsAssets.has(asset) && filterExistedAssets('script', asset),
294304
);
295305
const needPreloadCssAssets = cssAssets.filter(
296-
(asset) => !loadedSharedCssAssets.has(asset),
306+
(asset) =>
307+
!loadedSharedCssAssets.has(asset) && filterExistedAssets('link', asset),
297308
);
298309

299310
return {
300311
cssAssets: needPreloadCssAssets,
301312
jsAssetsWithoutEntry: needPreloadJsAssets,
302-
entryAssets,
313+
entryAssets: entryAssets.filter((entry) =>
314+
filterExistedAssets('script', entry.url),
315+
),
303316
};
304317
}
305318

@@ -316,6 +329,13 @@ export const generatePreloadAssetsPlugin: () => FederationRuntimePlugin =
316329
globalSnapshot,
317330
remoteSnapshot,
318331
} = args;
332+
if (!isBrowserEnv()) {
333+
return {
334+
cssAssets: [],
335+
jsAssetsWithoutEntry: [],
336+
entryAssets: [],
337+
};
338+
}
319339

320340
if (isRemoteInfoWithEntry(remote) && isPureRemoteEntry(remote)) {
321341
return {

0 commit comments

Comments
 (0)