Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 catch javascript runtime error #425

Open
wants to merge 6 commits into
base: release/2.7.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark",
"version": "2.6.1",
"version": "2.6.2",
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
"scripts": {
"install:deps": "rm -rf node_modules && rm -rf ./packages/*/node_modules && yarn install && lerna exec -- npm install",
Expand Down
33 changes: 33 additions & 0 deletions src/util/handleAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ export interface ILifecycleProps {
customProps?: object;
}

function isAssetExist(element: HTMLScriptElement | HTMLLIElement, type: 'script' | 'link') {
const urlAlias = type === 'script' ? 'src' : 'href';

return Array.from(document.getElementsByTagName(type))
.some((item) => {
if (
item[urlAlias]
&& element[urlAlias] === item[urlAlias]
) {
return true;
}
return false;
});
}

/**
* Create link/style element and append to root
*/
Expand Down Expand Up @@ -228,13 +243,31 @@ export function appendExternalScript(asset: string | Asset,
return;
}

if (isAssetExist(element, 'script')) {
resolve();
}

setAttributeForScriptNode(element, {
module,
id,
src: content || (asset as string),
scriptAttributes,
});

/**
* For JavaScript runtime error may not fire error event (https://github.com/ice-lab/icestark/issues/419).
* While window.onerror is supported to catch JavaScript runtime error.
*/
const rejectRuntimeError = (event: ErrorEvent) => {
if (event?.filename === (content || asset)) {
reject(new Error(`js asset runtime error: ${event?.message}`));
}

window.removeEventListener('error', rejectRuntimeError);
};

window.addEventListener('error', rejectRuntimeError);

element.addEventListener(
'error',
() => reject(new Error(`js asset loaded error: ${content || asset}`)),
Expand Down