Skip to content

Commit

Permalink
fix: throw RUNTIME-008 Error when script resources load failed
Browse files Browse the repository at this point in the history
  • Loading branch information
danpeen committed Dec 12, 2024
1 parent fa7a0bd commit d03b49c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/smart-crabs-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@module-federation/error-codes': patch
'@module-federation/runtime': patch
'@module-federation/sdk': patch
---

fix: throw RUNTIME-008 Error when script resources load failed
2 changes: 2 additions & 0 deletions packages/error-codes/src/desc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RUNTIME_005,
RUNTIME_006,
RUNTIME_007,
RUNTIME_008,
TYPE_001,
} from './error-codes';

Expand All @@ -17,6 +18,7 @@ export const runtimeDescMap = {
[RUNTIME_005]: 'Invalid loadShareSync function call from bundler runtime',
[RUNTIME_006]: 'Invalid loadShareSync function call from runtime',
[RUNTIME_007]: 'Failed to get remote snapshot.',
[RUNTIME_008]: 'Failed to load script resources.',
};

export const typeDescMap = {
Expand Down
1 change: 1 addition & 0 deletions packages/error-codes/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const RUNTIME_004 = 'RUNTIME-004';
export const RUNTIME_005 = 'RUNTIME-005';
export const RUNTIME_006 = 'RUNTIME-006';
export const RUNTIME_007 = 'RUNTIME-007';
export const RUNTIME_008 = 'RUNTIME-008';

export const TYPE_001 = 'TYPE-001';
14 changes: 14 additions & 0 deletions packages/runtime/src/utils/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Remote, RemoteEntryExports, RemoteInfo } from '../type';
import { assert } from './logger';
import {
RUNTIME_001,
RUNTIME_008,
getShortErrorMsg,
runtimeDescMap,
} from '@module-federation/error-codes';
Expand Down Expand Up @@ -129,6 +130,19 @@ async function loadEntryScript({
return entryExports;
})
.catch((e) => {
// const errMsg = getShortErrorMsg(RUNTIME_008, runtimeDescMap, {
// remoteName: name,
// remoteEntryUrl: entry,
// });
assert(
undefined,
getShortErrorMsg(RUNTIME_008, runtimeDescMap, {
remoteName: name,
resourceUrl: entry,
}),
);
// console.error('----loadScript error-----', entry);
// console.error(errMsg);
throw e;
});
}
Expand Down
45 changes: 35 additions & 10 deletions packages/sdk/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ export function isStaticResourcesEqual(url1: string, url2: string): boolean {

export function createScript(info: {
url: string;
cb?: (value: void | PromiseLike<void>) => void;
cb?: {
resolve: (value: void | PromiseLike<void>) => void;
reject: (error: Error) => void;
};
attrs?: Record<string, any>;
needDeleteScript?: boolean;
createScriptHook?: CreateScriptHookDom;
}): { script: HTMLScriptElement; needAttach: boolean } {
// Retrieve the existing script element by its src attribute
let script: HTMLScriptElement | null = null;
let needAttach = true;
let timeout = 20000;
let timeout = 60000;
let timeoutId: NodeJS.Timeout;
const scripts = document.getElementsByTagName('script');

for (let i = 0; i < scripts.length; i++) {
const s = scripts[i];
const scriptSrc = s.getAttribute('src');
Expand Down Expand Up @@ -88,6 +92,14 @@ export function createScript(info: {
event: any,
): Promise<void> => {
clearTimeout(timeoutId);
const onScriptCompleteCallback = () => {
if (event.type === 'error') {
info?.cb?.reject && info?.cb?.reject(event);
} else {
info?.cb?.resolve && info?.cb?.resolve();
}
};

// Prevent memory leaks in IE.
if (script) {
script.onerror = null;
Expand All @@ -102,14 +114,14 @@ export function createScript(info: {
const result = (prev as any)(event);
if (result instanceof Promise) {
const res = await result;
info?.cb?.();
onScriptCompleteCallback();
return res;
}
info?.cb?.();
onScriptCompleteCallback();
return result;
}
}
info?.cb?.();
onScriptCompleteCallback();
};

script.onerror = onScriptComplete.bind(null, script.onerror);
Expand All @@ -127,7 +139,10 @@ export function createScript(info: {

export function createLink(info: {
url: string;
cb: (value: void | PromiseLike<void>) => void;
cb?: {
resolve: (value: void | PromiseLike<void>) => void;
reject: (error: Error) => void;
};
attrs: Record<string, string>;
needDeleteLink?: boolean;
createLinkHook?: (
Expand Down Expand Up @@ -184,6 +199,13 @@ export function createLink(info: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
event: any,
): void => {
const onLinkCompleteCallback = () => {
if (event.type === 'error') {
info?.cb?.reject && info?.cb?.reject(event);
} else {
info?.cb?.resolve && info?.cb?.resolve();
}
};
// Prevent memory leaks in IE.
if (link) {
link.onerror = null;
Expand All @@ -197,11 +219,11 @@ export function createLink(info: {
if (prev) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res = (prev as any)(event);
info.cb();
onLinkCompleteCallback();
return res;
}
}
info.cb();
onLinkCompleteCallback();
};

link.onerror = onLinkComplete.bind(null, link.onerror);
Expand All @@ -218,10 +240,13 @@ export function loadScript(
},
) {
const { attrs = {}, createScriptHook } = info;
return new Promise<void>((resolve, _reject) => {
return new Promise<void>((resolve, reject) => {
const { script, needAttach } = createScript({
url,
cb: resolve,
cb: {
resolve,
reject,
},
attrs: {
fetchpriority: 'high',
...attrs,
Expand Down

0 comments on commit d03b49c

Please sign in to comment.