@@ -26,6 +26,7 @@ export function isStaticResourcesEqual(url1: string, url2: string): boolean {
2626export function createScript ( info : {
2727 url : string ;
2828 cb ?: ( value : void | PromiseLike < void > ) => void ;
29+ onErrorCallback ?: ( error : Error ) => void ;
2930 attrs ?: Record < string , any > ;
3031 needDeleteScript ?: boolean ;
3132 createScriptHook ?: CreateScriptHookDom ;
@@ -36,6 +37,7 @@ export function createScript(info: {
3637 let timeout = 20000 ;
3738 let timeoutId : NodeJS . Timeout ;
3839 const scripts = document . getElementsByTagName ( 'script' ) ;
40+
3941 for ( let i = 0 ; i < scripts . length ; i ++ ) {
4042 const s = scripts [ i ] ;
4143 const scriptSrc = s . getAttribute ( 'src' ) ;
@@ -88,6 +90,14 @@ export function createScript(info: {
8890 event : any ,
8991 ) : Promise < void > => {
9092 clearTimeout ( timeoutId ) ;
93+ const onScriptCompleteCallback = ( ) => {
94+ if ( event ?. type === 'error' ) {
95+ info ?. onErrorCallback && info ?. onErrorCallback ( event ) ;
96+ } else {
97+ info ?. cb && info ?. cb ( ) ;
98+ }
99+ } ;
100+
91101 // Prevent memory leaks in IE.
92102 if ( script ) {
93103 script . onerror = null ;
@@ -102,14 +112,14 @@ export function createScript(info: {
102112 const result = ( prev as any ) ( event ) ;
103113 if ( result instanceof Promise ) {
104114 const res = await result ;
105- info ?. cb ?. ( ) ;
115+ onScriptCompleteCallback ( ) ;
106116 return res ;
107117 }
108- info ?. cb ?. ( ) ;
118+ onScriptCompleteCallback ( ) ;
109119 return result ;
110120 }
111121 }
112- info ?. cb ?. ( ) ;
122+ onScriptCompleteCallback ( ) ;
113123 } ;
114124
115125 script . onerror = onScriptComplete . bind ( null , script . onerror ) ;
@@ -127,7 +137,8 @@ export function createScript(info: {
127137
128138export function createLink ( info : {
129139 url : string ;
130- cb : ( value : void | PromiseLike < void > ) => void ;
140+ cb ?: ( value : void | PromiseLike < void > ) => void ;
141+ onErrorCallback ?: ( error : Error ) => void ;
131142 attrs : Record < string , string > ;
132143 needDeleteLink ?: boolean ;
133144 createLinkHook ?: (
@@ -184,6 +195,13 @@ export function createLink(info: {
184195 // eslint-disable-next-line @typescript-eslint/no-explicit-any
185196 event : any ,
186197 ) : void => {
198+ const onLinkCompleteCallback = ( ) => {
199+ if ( event ?. type === 'error' ) {
200+ info ?. onErrorCallback && info ?. onErrorCallback ( event ) ;
201+ } else {
202+ info ?. cb && info ?. cb ( ) ;
203+ }
204+ } ;
187205 // Prevent memory leaks in IE.
188206 if ( link ) {
189207 link . onerror = null ;
@@ -197,11 +215,11 @@ export function createLink(info: {
197215 if ( prev ) {
198216 // eslint-disable-next-line @typescript-eslint/no-explicit-any
199217 const res = ( prev as any ) ( event ) ;
200- info . cb ( ) ;
218+ onLinkCompleteCallback ( ) ;
201219 return res ;
202220 }
203221 }
204- info . cb ( ) ;
222+ onLinkCompleteCallback ( ) ;
205223 } ;
206224
207225 link . onerror = onLinkComplete . bind ( null , link . onerror ) ;
@@ -218,10 +236,11 @@ export function loadScript(
218236 } ,
219237) {
220238 const { attrs = { } , createScriptHook } = info ;
221- return new Promise < void > ( ( resolve , _reject ) => {
239+ return new Promise < void > ( ( resolve , reject ) => {
222240 const { script, needAttach } = createScript ( {
223241 url,
224242 cb : resolve ,
243+ onErrorCallback : reject ,
225244 attrs : {
226245 fetchpriority : 'high' ,
227246 ...attrs ,
0 commit comments