Skip to content

Commit abbd39e

Browse files
committed
fix: fix load js elemnet error event
1 parent 2457ded commit abbd39e

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

packages/runtime/browser-vm/src/dynamicNode/processor.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,23 @@ export class DynamicNodeProcessor {
6060
}
6161

6262
// Put it in the next macro task to ensure that the current synchronization script is executed
63-
private dispatchEvent(type: string) {
63+
private dispatchEvent(type: string, errInfo?: ErrorEventInit) {
6464
setTimeout(() => {
65-
const event: Event & { garfish?: boolean } = new Event(type);
66-
event.garfish = true;
65+
const isError = type === 'error';
66+
let event: Event & { __byGarfish__?: boolean };
67+
68+
if (isError) {
69+
event = new ErrorEvent(type, {
70+
...errInfo,
71+
message: errInfo.error.message,
72+
});
73+
} else {
74+
event = new Event(type);
75+
}
76+
event.__byGarfish__ = true;
6777
Object.defineProperty(event, 'target', { value: this.el });
6878
this.el.dispatchEvent(event);
69-
type === 'error' && window.dispatchEvent(event);
79+
isError && window.dispatchEvent(event);
7080
});
7181
}
7282

@@ -88,7 +98,10 @@ export class DynamicNodeProcessor {
8898
})
8999
.catch((e) => {
90100
__DEV__ && warn(e);
91-
this.dispatchEvent('error');
101+
this.dispatchEvent('error', {
102+
error: e,
103+
filename: fetchUrl,
104+
});
92105
});
93106
}
94107
} else {
@@ -112,12 +125,18 @@ export class DynamicNodeProcessor {
112125
this.sandbox.loader
113126
.load<JavaScriptManager>(namespace, fetchUrl)
114127
.then(({ resourceManager: { url, scriptCode } }) => {
115-
this.dispatchEvent('load');
116-
this.sandbox.execScript(scriptCode, {}, url, { noEntry: true });
128+
// It is necessary to ensure that the code execution error cannot trigger the `el.onerror` event
129+
setTimeout(() => {
130+
this.dispatchEvent('load');
131+
this.sandbox.execScript(scriptCode, {}, url, { noEntry: true });
132+
});
117133
})
118134
.catch((e) => {
119135
__DEV__ && warn(e);
120-
this.dispatchEvent('error');
136+
this.dispatchEvent('error', {
137+
error: e,
138+
filename: fetchUrl,
139+
});
121140
});
122141
} else if (code) {
123142
this.sandbox.execScript(code, {}, baseUrl, { noEntry: true });

0 commit comments

Comments
 (0)