Merge pull request #16604 from keymanapp/fix/web/13862-retry-networkerror-on-importScripts

fix(web): retry importScripts NetworkErrors in attempt to mitigate errors loading models 🏠
This commit is contained in:
Marc Durdin 2026-09-21 15:02:45 +07:00 • committed by GitHub
commit 87e87e1ff8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -242,7 +242,7 @@ export default class LMLayerWorker {
}
}
private loadModelFile(url: string) {
private loadModelFile(url: string, tryCount: number = 0) {
// The self/global WebWorker method, allowing us to directly import another script file into WebWorker scope.
// If built correctly, the model's script file will auto-register the model with loadModel() above.
try {
@ -250,8 +250,19 @@ export default class LMLayerWorker {
} catch (err) {
// Does not catch errors thrown within the imported script.
// Does catch errors with the model script's file-path.
this.error("Error occurred when attempting to load dictionary", err);
// #13862: attempt to mitigate NetworkError on Chrome by retrying before giving up
tryCount++;
if(err instanceof Error && err.name == 'NetworkError') {
if(tryCount < 4) {
// increase delay with each retry: 10msec, 100msec, 1000msec
console?.warn(`NetworkError when attempting to load dictionary (attempt ${tryCount} of 4, will retry after ${10 ** tryCount} msec): ${err.toString()}`);
setTimeout(() => this.loadModelFile(url, tryCount), 10 ** tryCount);
} else {
this.error(`NetworkError when attempting to load dictionary (attempt ${tryCount} of 4, giving up): ${err.toString()}`);
}
} else {
this.error("Error occurred when attempting to load dictionary", err);
}
// Remain in the model-unloaded state; the load attempt was unsuccessful.
}
}