Skip to content

Commit e58ebe9

Browse files
committed
fix: improve runtime asset handling when runtime is loaded
1 parent bc362e8 commit e58ebe9

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ See [https://github.com/ice-lab/icestark/releases](https://github.com/ice-lab/ic
55
# 2.8.4
66

77
- [fix] automatically switch to "fetch" mode when runtime is set.
8+
- [fix] improve runtime asset handling when runtime is loaded.
89

910
# 2.8.3
1011

packages/icestark/src/apps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export async function loadAppModule(appConfig: AppConfig) {
218218
const runtimeJsList = [];
219219
// Filter and map runtime libraries that haven't been registered in window
220220
const runtimeLibs = runtime?.filter((config) => {
221-
return config.version && config.library && !window[`${config.library}@${config.version}`];
221+
return config.version && config.library;
222222
}).map((config) => {
223223
// Handle both string and array URL formats
224224
const urls = Array.isArray(config.url) ? config.url : [config.url];
@@ -250,6 +250,7 @@ export async function loadAppModule(appConfig: AppConfig) {
250250
return {
251251
content: mainJs,
252252
type: AssetTypeEnum.RUNTIME,
253+
loaded: Boolean(config.version && config.library && window[`${config.library}@${config.version}`]),
253254
library: config.library,
254255
version: config.version,
255256
};

packages/icestark/src/util/handleAssets.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export enum AssetCommentEnum {
3333

3434
export interface Asset {
3535
module?: boolean;
36+
loaded?: boolean;
3637
type: AssetTypeEnum;
3738
/** Only used when type is AssetTypeEnum.RUNTIME */
3839
library?: string;
@@ -313,19 +314,20 @@ export function getUrlAssets(urls: string | string[]) {
313314
export async function fetchScripts(jsList: Asset[], fetch: Fetch = defaultFetch): Promise<string[]> {
314315
let jsBeforeRuntime = '';
315316
let jsAfterRuntime = '';
316-
317317
jsList.forEach((asset) => {
318318
if (asset.type === AssetTypeEnum.RUNTIME) {
319319
const { library, version } = asset;
320320
const globalLib = `window['${library}']`;
321321
const backupLib = `window['__${library}__']`;
322322
const versionedLib = `window['${library}@${version}']`;
323-
jsBeforeRuntime = `${jsBeforeRuntime}if (${globalLib}) {${backupLib} = ${globalLib};}\n`;
324-
jsAfterRuntime = `${jsAfterRuntime}${versionedLib} = ${globalLib}; if (${backupLib}) {${globalLib} = ${backupLib};${backupLib} = undefined;}\n`;
323+
const backupCode = `if (${globalLib}) {${backupLib} = ${globalLib};}\n`;
324+
const restoreCode = `if (${backupLib}) {${globalLib} = ${backupLib};${backupLib} = undefined;}\n`;
325+
jsBeforeRuntime = `${jsBeforeRuntime}${backupCode}${asset.loaded ? `${globalLib} = ${versionedLib};` : ''}`;
326+
jsAfterRuntime = `${jsAfterRuntime}${asset.loaded ? '' : `${versionedLib} = ${globalLib};`}${restoreCode}`;
325327
}
326328
});
327329

328-
const result = await Promise.all(jsList.map(async (asset) => {
330+
const result = await Promise.all(jsList.filter((asset) => !asset.loaded).map(async (asset) => {
329331
const { type, content } = asset;
330332
if (type === AssetTypeEnum.INLINE) {
331333
return {

0 commit comments

Comments
 (0)