Skip to content

Commit

Permalink
DEV: Fix wasm_bindgen double loading errors in Firefox
Browse files Browse the repository at this point in the history
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 w3c/ServiceWorker#1041)
  • Loading branch information
martin-brennan committed Oct 18, 2021
1 parent 235d069 commit 0ec4e92
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions public/javascripts/media-optimization-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0ec4e92

Please sign in to comment.