From 3f0782d1cf89103e885b81e4fc958f17898e2865 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 2 Jun 2024 06:24:10 +0200 Subject: [PATCH] Add workaround for 429 Too Many Requests Firefox has an annoying bug that they refuse to fix where returning a valid 429 response is considered an aborted connection. Firefox makes no additional attempts at retrying, no matter what the request may have been. Chromium and even Internet Explorer handle this correctly. --- assets/js/highlight.mjs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/assets/js/highlight.mjs b/assets/js/highlight.mjs index 102f432..7193b24 100644 --- a/assets/js/highlight.mjs +++ b/assets/js/highlight.mjs @@ -74,9 +74,32 @@ async function initializeHighlightJS() { let langs = []; for (let lang of languages) { console.log(`highlight.JS: Importing language definition for '${lang}'...`); - langs.push(import(`./highlightjs/languages/${lang}.min.js`).then((mod) => { - window.highlightJS.registerLanguage(lang, mod.default); - console.log(`highlight.JS: Imported language definition for '${lang}'.`); + langs.push(new Promise(async (resolve, reject) => { + const start = performance.now(); + const max_attempts = 5; + let attempt = 0; + + let mod = undefined; + let err = null; + for (let attempt = 1; attempt <= 5; attempt++) { + try { + mod = await import(`./highlightjs/languages/${lang}.min.js`); + break; + } catch(ex) { + err = ex; + await new Promise((resolve, reject) => { + setTimeout(() => {resolve()}, 1000); + }) + } + } + + if (!mod) { + reject(err); + } else { + window.highlightJS.registerLanguage(lang, mod.default); + console.log(`highlight.JS: Imported language definition for '${lang}' after ${(performance.now() - start).toFixed(2)}ms.`); + resolve(mod); + } })); } await Promise.allSettled(langs)