From b69c2f7311836b8e35719a4d24cb60020fd7234d Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 18 Oct 2021 15:47:50 +1000 Subject: [PATCH] DEV: Fix wasm_bindgen double loading errors in Firefox (#14630) When we are calling the loadLibs function, which in turn calls: importScripts(settings.mozjpeg_script); importScripts(settings.resize_script); For the media-optimization-worker service worker, we are getting an error in Firefox, which balks at wasm_bindgen, a global variable defined with let, being redefined when the module loads. This causes image processing to fail in Firefox when more than one image is uploaded at a time. The solution to this is to just check whether the scripts are already imported, and if so do not import them again. Chrome doesn't seem to care about this variable redefinition and does not error, and it seems to be expected behaviour that the script can be loaded multiple times (see https://github.com/w3c/ServiceWorker/issues/1041) --- public/javascripts/media-optimization-worker.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/public/javascripts/media-optimization-worker.js b/public/javascripts/media-optimization-worker.js index 189a88b761f..e57e3627239 100644 --- a/public/javascripts/media-optimization-worker.js +++ b/public/javascripts/media-optimization-worker.js @@ -155,8 +155,15 @@ async function loadLibs(settings){ if (self.codecs) return; - importScripts(settings.mozjpeg_script); - importScripts(settings.resize_script); + if (!self.loadedMozJpeg) { + importScripts(settings.mozjpeg_script); + self.loadedMozJpeg = true; + } + + if (!self.loadedResizeScript) { + importScripts(settings.resize_script); + self.loadedResizeScript = true; + } let encoderModuleOverrides = { locateFile: function(path, prefix) {