diff --git a/packages/desktop-client/src/browser-server.js b/packages/desktop-client/src/browser-server.js index 8ccee8c3402769551ef5c93da51aa5733a5cb8ff..7ac16a3e7e302d722a4ebffa24bf55ed9023ab3f 100644 --- a/packages/desktop-client/src/browser-server.js +++ b/packages/desktop-client/src/browser-server.js @@ -1,7 +1,42 @@ /* globals importScripts, backend */ let hasInitialized = false; -self.addEventListener('message', e => { +/** + * Sometimes the frontend build is way faster than backend. + * This results in the frontend starting up before backend is + * finished and thus the backend script is not available. + * + * The goal of this function is to retry X amount of times + * to retrieve the backend script with a small delay. + */ +const importScriptsWithRetry = async (script, { maxRetries = 5 } = {}) => { + try { + importScripts(script); + } catch (e) { + // Break if maxRetries has exceeded + if (maxRetries <= 0) { + throw e; + } else { + console.groupCollapsed( + `Failed to load backend, will retry ${maxRetries} more time(s)`, + ); + console.log(e); + console.groupEnd(); + } + + // Attempt to retry after a small delay + await new Promise(resolve => + setTimeout(async () => { + await importScriptsWithRetry(script, { + maxRetries: maxRetries - 1, + }); + resolve(); + }, 5000), + ); + } +}; + +self.addEventListener('message', async e => { if (!hasInitialized) { let msg = e.data; @@ -19,7 +54,10 @@ self.addEventListener('message', e => { return; } - importScripts(`${msg.publicUrl}/kcab/kcab.worker.${hash}.js`); + await importScriptsWithRetry( + `${msg.publicUrl}/kcab/kcab.worker.${hash}.js`, + { maxRetries: isDev ? 5 : 0 }, + ); backend.initApp(version, isDev, self).catch(err => { console.log(err); diff --git a/upcoming-release-notes/806.md b/upcoming-release-notes/806.md new file mode 100644 index 0000000000000000000000000000000000000000..7c4edfcc4809485079b1d2ba546a97de391bdf43 --- /dev/null +++ b/upcoming-release-notes/806.md @@ -0,0 +1,6 @@ +--- +category: Maintenance +authors: [MatissJanis] +--- + +Retry loading backend script in web-workers (for local dev server)