Unity DataCaching (IndexedDB) not working for .wasm file

Our target platform is TVs that clear the cache for web applications, so a possible way out for us is the use of IndexedDB, for which the Data Caching parameter when compiling WebGL is responsible, but judging by the logs and the page loading debug (Network panel), it works only for the .data file

I already tried override cacheControl method, but it didn’t help either, because it is responsible for files (like .data) requested from loader.js, and file .wasm is requested from .framework.js

So .wasm can only always be downloaded from server or cached by the browser, but with the TV platform it’s the only first option, which really cuts down on load times (~1min for ~10mb build).

Unity Editor 2021.1.28 (last with asm.js support - we targeting both asm and wasm)

Is there any option to force .wasm to be “cached” in the IndexedDB like .data?

I can’t really answer the question but the support of asm.js in this day and age raises my eyebrows, so excuse me for raising the question whether this is absolutely necessary. :wink:

At least explore newer versions - not commit yet. At least 2021.3 LTS but try going even higher. A lot of progress has been made in Web tech on Unity’s side, including load times and caching. It’s definitely worth investigating, especially if it solves the issue you could use that argue in favor of dropping asm.js support.

Unless you have a requirement for asm.js in the sense “wouldn’t work otherwise on a given hardware” I would straight out drop asm support. Even then, such a requirement is often to satisfy marketing statistics (aka a few percent more potential customers due to device market share) but not the reality (none of these “potential customers” even exist, because these outdated devices, even though still being tracked in statistics, have likely been moved off to secondary uses like a kitchen TV for recipes, kid’s room TV set, stored in a cupboard drawer for “hard times”, etc).

If it means a better experience for 95% of your target audience and lose 5% due to incompatibility, that’s a good tradeoff. :wink:

1 Like

The problem is not with the version of Unity, because the behavior is the same on version 2022.3.17. Of course, if caching works in the browser, then you can simply turn off Data Caching in the settings and configure the appropriate HTTP headers, but when this cache is constantly cleared on TVs, I need “caching” using IndexedDB, which this Unity setting provides. Debugging examples via the Network tab below.

Here you can see that the size of the .data file is “significant” (1.3MB), because this is the first run and it was being downloaded.
[UnityCache] ‘http://localhost/Build/Build%202022**.data**.gz’ successfully downloaded and stored in the indexedDB cache
9593845--1359709--upload_2024-1-20_16-21-26.png

At the second launch, the size is formal (the data passed validation and was loaded from IndexedDB), but only for the .data file, and I need the same to work for .wasm and .asm.js files (now they download over the network every time).
[UnityCache] ‘http://localhost/Build/Build%202022**.data**.gz’ successfully revalidated and served from the indexedDB cache
9593845--1359706--upload_2024-1-20_16-20-34.png

About asm.js (asm) and WebAssembly (wasm)
https://webostv.developer.lge.com/develop/specifications/web-api-and-web-engine - WebOS supports WASM from ~2019 (not so old)
https://developer.samsung.com/smarttv/develop/specifications/web-engine-specifications.html - the same for Tizen

I build both versions and the appropriate one is delivered to the user

I believe that you can add your own caching logic inside the .framework.js file, namely in a specific method integrateWasmJS()

I have limited knowledge in web and javascript
If I can achieve the result by manually changing the source .framework.js file of build output, then I can override this method using the .jslib plugin

It is possible? Any ideas?

function integrateWasmJS() {
        var wasmTextFile = "build.wast";
        var wasmBinaryFile = "build.wasm";
        var asmjsCodeFile = "build.temp.asm.js";
        if (!isDataURI(wasmTextFile)) {
            wasmTextFile = locateFile(wasmTextFile)
        }
        if (!isDataURI(wasmBinaryFile)) {
            wasmBinaryFile = locateFile(wasmBinaryFile)
        }
        if (!isDataURI(asmjsCodeFile)) {
            asmjsCodeFile = locateFile(asmjsCodeFile)
        }
        var wasmPageSize = 64 * 1024;
        var info = {
            "global": null,
            "env": null,
            "asm2wasm": asm2wasmImports,
            "parent": Module
        };
        var exports = null;

        function mergeMemory(newBuffer) {
            var oldBuffer = Module["buffer"];
            if (newBuffer.byteLength < oldBuffer.byteLength) {
                err("the new buffer in mergeMemory is smaller than the previous one. in native wasm, we should grow memory here")
            }
            var oldView = new Int8Array(oldBuffer);
            var newView = new Int8Array(newBuffer);
            newView.set(oldView);
            updateGlobalBuffer(newBuffer);
            updateGlobalBufferViews()
        }

        function fixImports(imports) {
            return imports
        }

        function getBinary() {
            try {
                if (Module["wasmBinary"]) {
                    return new Uint8Array(Module["wasmBinary"])
                }
                if (Module["readBinary"]) {
                    return Module["readBinary"](wasmBinaryFile)
                } else {
                    throw "both async and sync fetching of the wasm failed"
                }
            } catch (err) {
                abort(err)
            }
        }

        function getBinaryPromise() {
            if (!Module["wasmBinary"] && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === "function") {
                return fetch(wasmBinaryFile, {
                    credentials: "same-origin"
                }).then((function(response) {
                    if (!response["ok"]) {
                        throw "failed to load wasm binary file at '" + wasmBinaryFile + "'"
                    }
                    return response["arrayBuffer"]()
                })).catch((function() {
                    return getBinary()
                }))
            }
            return new Promise((function(resolve, reject) {
                resolve(getBinary())
            }))
        }
        //test start
       
        //test end


        function doNativeWasm(global, env, providedBuffer) {
            if (typeof WebAssembly !== "object") {
                err("no native wasm support detected");
                return false
            }
            if (!(Module["wasmMemory"] instanceof WebAssembly.Memory)) {
                err("no native wasm Memory in use");
                return false
            }
            env["memory"] = Module["wasmMemory"];
            info["global"] = {
                "NaN": NaN,
                "Infinity": Infinity
            };
            info["global.Math"] = Math;
            info["env"] = env;

            function receiveInstance(instance, module) {
                console.warn("[FFFF] instantiateArrayBuffer" + instance + " | " + module)
                exports = instance.exports;
                if (exports.memory) mergeMemory(exports.memory);
                Module["asm"] = exports;
                Module["usingWasm"] = true;
                removeRunDependency("wasm-instantiate")
            }
            addRunDependency("wasm-instantiate");
            if (Module["instantiateWasm"]) {
                try {
                    return Module["instantiateWasm"](info, receiveInstance)
                } catch (e) {
                    err("Module.instantiateWasm callback failed with error: " + e);
                    return false
                }
            }

            function receiveInstantiatedSource(output) {
               
                console.warn("[FFFF] instantiateArrayBuffer" + output)
                receiveInstance(output["instance"], output["module"])
            }

            function instantiateArrayBuffer(receiver) {
                console.warn("[FFFF] instantiateArrayBuffer" + receiver)
                getBinaryPromise().then((function(binary) {
                    return WebAssembly.instantiate(binary, info)
                })).then(receiver).catch((function(reason) {
                    err("failed to asynchronously prepare wasm: " + reason);
                    abort(reason)
                }))
            }
            if (!Module["wasmBinary"] && typeof WebAssembly.instantiateStreaming === "function" && !isDataURI(wasmBinaryFile) && typeof fetch === "function")
            {
                console.log("[FFFF-1]" + wasmBinaryFile);
                WebAssembly.instantiateStreaming(fetch(wasmBinaryFile,
                    {
                        credentials: "same-origin"
                    }),
                    info).then(receiveInstantiatedSource).catch((function(reason)
                    {
                        err("wasm streaming compile failed: " + reason);
                        err("falling back to ArrayBuffer instantiation");
                        instantiateArrayBuffer(receiveInstantiatedSource)
                    }))
            }
            else
            {
                console.log("[FFFF-2]" + wasmBinaryFile);
                instantiateArrayBuffer(receiveInstantiatedSource)
            }
            return {}
        }
        Module["asmPreload"] = Module["asm"];
        var asmjsReallocBuffer = Module["reallocBuffer"];
        var wasmReallocBuffer = (function(size) {
            var PAGE_MULTIPLE = Module["usingWasm"] ? WASM_PAGE_SIZE : ASMJS_PAGE_SIZE;
            size = alignUp(size, PAGE_MULTIPLE);
            var old = Module["buffer"];
            var oldSize = old.byteLength;
            if (Module["usingWasm"]) {
                try {
                    var result = Module["wasmMemory"].grow((size - oldSize) / wasmPageSize);
                    if (result !== (-1 | 0)) {
                        return Module["buffer"] = Module["wasmMemory"].buffer
                    } else {
                        return null
                    }
                } catch (e) {
                    return null
                }
            }
        });
        Module["reallocBuffer"] = (function(size) {
            if (finalMethod === "asmjs") {
                return asmjsReallocBuffer(size)
            } else {
                return wasmReallocBuffer(size)
            }
        });
        var finalMethod = "";
        Module["asm"] = (function(global, env, providedBuffer) {
            env = fixImports(env);
            if (!env["table"]) {
                var TABLE_SIZE = Module["wasmTableSize"];
                if (TABLE_SIZE === undefined) TABLE_SIZE = 1024;
                var MAX_TABLE_SIZE = Module["wasmMaxTableSize"];
                if (typeof WebAssembly === "object" && typeof WebAssembly.Table === "function") {
                    if (MAX_TABLE_SIZE !== undefined) {
                        env["table"] = new WebAssembly.Table({
                            "initial": TABLE_SIZE,
                            "maximum": MAX_TABLE_SIZE,
                            "element": "anyfunc"
                        })
                    } else {
                        env["table"] = new WebAssembly.Table({
                            "initial": TABLE_SIZE,
                            element: "anyfunc"
                        })
                    }
                } else {
                    env["table"] = new Array(TABLE_SIZE)
                }
                Module["wasmTable"] = env["table"]
            }
            if (!env["memoryBase"]) {
                env["memoryBase"] = Module["STATIC_BASE"]
            }
            if (!env["tableBase"]) {
                env["tableBase"] = 0
            }
            var exports;
            exports = doNativeWasm(global, env, providedBuffer);
            assert(exports, "no binaryen method succeeded.");
            return exports
        });
    }

Some update. If you set the Decompression Fallback option together with some compression, then .wasm file is also requested from .loader.js - accordingly, the override cacheControl works and the .wasm file is cached in IndexedDB, just like the .data, but what if I want to do it without Decompression Fallback? Does this one option not cause problems and can it be left? (The headers on the server are configured, so standard browser compression methods will be used if possible and the speed will not decrease?)

Maybe the answer is hidden in UnityLoader.js? (internal editor file …\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\UnityLoader\UnityLoader.js)

function createUnityInstance(canvas, config, onProgress) {
  onProgress = onProgress || function () {};

#if USE_THREADS
  // Polyfill Atomics.wake for old Emscripten fastcomp compiler.
  // TODO: When we update to new Emscripten, this can be removed.
  if (typeof Atomics !== 'undefined' && Atomics.notify && !Atomics.wake) {
    Atomics.wake = Atomics.notify;
  }
#endif

  function showBanner(msg, type) {
    // Only ever show one error at most - other banner messages after that should get ignored
    // to avoid noise.
    if (!showBanner.aborted && config.showBanner) {
      if (type == 'error') showBanner.aborted = true;
      return config.showBanner(msg, type);
    }

    // Fallback to console logging if visible banners have been suppressed
    // from the main page.
    switch(type) {
      case 'error': console.error(msg); break;
      case 'warning': console.warn(msg); break;
      default: console.log(msg); break;
    }
  }

  function errorListener(e) {
    var error = e.reason || e.error;
    var message = error ? error.toString() : (e.message || e.reason || '');
    var stack = (error && error.stack) ? error.stack.toString() : '';

    // Do not repeat the error message if it's present in the stack trace.
    if (stack.startsWith(message)) {
      stack = stack.substring(message.length);
    }

    message += '\n' + stack.trim();

    if (!message || !Module.stackTraceRegExp || !Module.stackTraceRegExp.test(message))
      return;

    var filename = e.filename || (error && (error.fileName || error.sourceURL)) || '';
    var lineno = e.lineno || (error && (error.lineNumber || error.line)) || 0;

#if SYMBOLS_FILENAME
    demanglingErrorHandler(message, filename, lineno);
#else // SYMBOLS_FILENAME
    errorHandler(message, filename, lineno);
#endif // SYMBOLS_FILENAME
  }

  var Module = {
    canvas: canvas,
    webglContextAttributes: {
      preserveDrawingBuffer: false,
    },
#if USE_DATA_CACHING
    cacheControl: function (url) {
      return url == Module.dataUrl ? "must-revalidate" : "no-store";
    },
#endif // USE_DATA_CACHING
#if !USE_WASM
    TOTAL_MEMORY: {{{ TOTAL_MEMORY }}},
#endif // !USE_WASM
    streamingAssetsUrl: "StreamingAssets",
    downloadProgress: {},
    deinitializers: [],
    intervals: {},
    setInterval: function (func, ms) {
      var id = window.setInterval(func, ms);
      this.intervals[id] = true;
      return id;
    },
    clearInterval: function(id) {
      delete this.intervals[id];
      window.clearInterval(id);
    },
    preRun: [],
    postRun: [],
    print: function (message) {
      console.log(message);
    },
    printErr: function (message) {
      console.error(message);

      if (typeof message === 'string' && message.indexOf('wasm streaming compile failed') != -1) {
        if (message.toLowerCase().indexOf('mime') != -1) {
          showBanner('HTTP Response Header "Content-Type" configured incorrectly on the server for file ' + Module.codeUrl + ' , should be "application/wasm". Startup time performance will suffer.', 'warning');
        } else {
          showBanner('WebAssembly streaming compilation failed! This can happen for example if "Content-Encoding" HTTP header is incorrectly enabled on the server for file ' + Module.codeUrl + ', but the file is not pre-compressed on disk (or vice versa). Check the Network tab in browser Devtools to debug server header configuration.', 'warning');
        }
      }
    },
    locateFile: function (url) {
      return (
#if USE_WASM && !DECOMPRESSION_FALLBACK
        url == "build.wasm" ? this.codeUrl :
#endif // USE_WASM && !DECOMPRESSION_FALLBACK
#if USE_THREADS
#if DECOMPRESSION_FALLBACK
        url == "pthread-main.js" ? this.frameworkBlobUrl :
#else // DECOMPRESSION_FALLBACK
        url == "pthread-main.js" ? this.frameworkUrl :
#endif // DECOMPRESSION_FALLBACK
#endif // USE_THREADS
        url
      );
    },
#if USE_THREADS
    // The contents of "pthread-main.js" is embedded in the framework, which is used as a worker source.
    // Therefore Module.mainScriptUrlOrBlob is no longer needed and is set to a dummy blob for compatibility reasons.
    mainScriptUrlOrBlob: new Blob([" "], { type: "application/javascript" }),
#endif // USE_THREADS
    disabledCanvasEvents: [
      "contextmenu",
      "dragstart",
    ],
  };

  for (var parameter in config)
    Module[parameter] = config[parameter];

  Module.streamingAssetsUrl = new URL(Module.streamingAssetsUrl, document.URL).href;

  // Operate on a clone of Module.disabledCanvasEvents field so that at Quit time
  // we will ensure we'll remove the events that we created (in case user has
  // modified/cleared Module.disabledCanvasEvents in between)
  var disabledCanvasEvents = Module.disabledCanvasEvents.slice();

  function preventDefault(e) {
    e.preventDefault();
  }

  disabledCanvasEvents.forEach(function (disabledCanvasEvent) {
    canvas.addEventListener(disabledCanvasEvent, preventDefault);
  });

  window.addEventListener("error", errorListener);
  window.addEventListener("unhandledrejection", errorListener);

  var unityInstance = {
    Module: Module,
    SetFullscreen: function () {
      if (Module.SetFullscreen)
        return Module.SetFullscreen.apply(Module, arguments);
      Module.print("Failed to set Fullscreen mode: Player not loaded yet.");
    },
    SendMessage: function () {
      if (Module.SendMessage)
        return Module.SendMessage.apply(Module, arguments);
      Module.print("Failed to execute SendMessage: Player not loaded yet.");
    },
    Quit: function () {
      return new Promise(function (resolve, reject) {
        Module.shouldQuit = true;
        Module.onQuit = resolve;

        // Clear the event handlers we added above, so that the event handler
        // functions will not hold references to this JS function scope after
        // exit, to allow JS garbage collection to take place.
        disabledCanvasEvents.forEach(function (disabledCanvasEvent) {
          canvas.removeEventListener(disabledCanvasEvent, preventDefault);
        });
        window.removeEventListener("error", errorListener);
        window.removeEventListener("unhandledrejection", errorListener);
      });
    },
  };

  Module.SystemInfo = (function () {
#if 0
    // Recognize and parse the following formats of user agents:

    // Opera 71 on Windows 10:         Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228
    // Edge 85 on Windows 10:          Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70
    // Firefox 81 on Windows 10:       Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
    // Chrome 85 on Windows 10:        Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
    // IE 11 on Windows 7:             Mozilla/5.0 CK={} (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
    // IE 10 on Windows 7:             Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)

    // Chrome 80 on Android 8.0.0:     Mozilla/5.0 (Linux; Android 8.0.0; VKY-L29) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36
    // Firefox 68 on Android 8.0.0:    Mozilla/5.0 (Android 8.0.0; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0

    // Samsung Browser on Android 9:   Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.2 Chrome/71.0.3578.99 Mobile Safari/537.36
    // Safari 13.0.5 on iPhone 13.3.1: Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1

    // Safari 12.1 on iPad OS 12.2     Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1

    // Safari 14 on macOS 11.0:        Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1
    // Safari 14 on macOS 10.15.6:     Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
    // Firefox 80 on macOS 10.15:      Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:80.0) Gecko/20100101 Firefox/80.0
    // Chrome 65 on macOS 10.15.6:     Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36

    // Firefox 57 on FreeBSD:          Mozilla/5.0 (X11; FreeBSD amd64; rv:57.0) Gecko/20100101 Firefox/57.0
    // Chrome 43 on OpenBSD:           Mozilla/5.0 (X11; OpenBSD amd64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36
#endif

    var browser, browserVersion, os, osVersion, canvas, gpu;

    var ua = navigator.userAgent + ' ';
    var browsers = [
      ['Firefox', 'Firefox'],
      ['OPR', 'Opera'],
      ['Edg', 'Edge'],
      ['SamsungBrowser', 'Samsung Browser'],
      ['Trident', 'Internet Explorer'],
      ['MSIE', 'Internet Explorer'],
      ['Chrome', 'Chrome'],
      ['CriOS', 'Chrome on iOS Safari'],
      ['FxiOS', 'Firefox on iOS Safari'],
      ['Safari', 'Safari'],
    ];

    function extractRe(re, str, idx) {
      re = RegExp(re, 'i').exec(str);
      return re && re[idx];
    }
    for(var b = 0; b < browsers.length; ++b) {
      browserVersion = extractRe(browsers[b][0] + '[\/ ](.*?)[ \\)]', ua, 1);
      if (browserVersion) {
        browser = browsers[b][1];
        break;
      }
    }
    if (browser == 'Safari') browserVersion = extractRe('Version\/(.*?) ', ua, 1);
    if (browser == 'Internet Explorer') browserVersion = extractRe('rv:(.*?)\\)? ', ua, 1) || browserVersion;

    var oses = [
      ['Windows (.*?)[;\)]', 'Windows'],
      ['Android ([0-9_\.]+)', 'Android'],
      ['iPhone OS ([0-9_\.]+)', 'iPhoneOS'],
      ['iPad.*? OS ([0-9_\.]+)', 'iPadOS'],
      ['FreeBSD( )', 'FreeBSD'],
      ['OpenBSD( )', 'OpenBSD'],
      ['Linux|X11()', 'Linux'],
      ['Mac OS X ([0-9_\.]+)', 'macOS'],
      ['bot|google|baidu|bing|msn|teoma|slurp|yandex', 'Search Bot']
    ];
    for(var o = 0; o < oses.length; ++o) {
      osVersion = extractRe(oses[o][0], ua, 1);
      if (osVersion) {
        os = oses[o][1];
        osVersion = osVersion.replace(/_/g, '.');
        break;
      }
    }
    var versionMappings = {
      'NT 5.0': '2000',
      'NT 5.1': 'XP',
      'NT 5.2': 'Server 2003',
      'NT 6.0': 'Vista',
      'NT 6.1': '7',
      'NT 6.2': '8',
      'NT 6.3': '8.1',
      'NT 10.0': '10'
    };
    osVersion = versionMappings[osVersion] || osVersion;

    // TODO: Add mobile device identifier, e.g. SM-G960U

    canvas = document.createElement("canvas");
    if (canvas) {
      gl = canvas.getContext("webgl2");
      glVersion = gl ? 2 : 0;
      if (!gl) {
        if (gl = canvas && canvas.getContext("webgl")) glVersion = 1;
      }

      if (gl) {
        gpu = (gl.getExtension("WEBGL_debug_renderer_info") && gl.getParameter(0x9246 /*debugRendererInfo.UNMASKED_RENDERER_WEBGL*/)) || gl.getParameter(0x1F01 /*gl.RENDERER*/);
      }
    }

    var hasThreads = typeof SharedArrayBuffer !== 'undefined';
    var hasWasm = typeof WebAssembly === "object" && typeof WebAssembly.compile === "function";
    return {
      width: screen.width,
      height: screen.height,
      userAgent: ua.trim(),
      browser: browser || 'Unknown browser',
      browserVersion: browserVersion || 'Unknown version',
      mobile: /Mobile|Android|iP(ad|hone)/.test(navigator.appVersion),
      os: os || 'Unknown OS',
      osVersion: osVersion || 'Unknown OS Version',
      gpu: gpu || 'Unknown GPU',
      language: navigator.userLanguage || navigator.language,
      hasWebGL: glVersion,
      hasCursorLock: !!document.body.requestPointerLock,
      hasFullscreen: !!document.body.requestFullscreen || !!document.body.webkitRequestFullscreen, // Safari still uses the webkit prefixed version
      hasThreads: hasThreads,
      hasWasm: hasWasm,
      // This should be updated when we re-enable wasm threads. Previously it checked for WASM thread
      // support with: var wasmMemory = hasWasm && hasThreads && new WebAssembly.Memory({"initial": 1, "maximum": 1, "shared": true});
      // which caused Chrome to have a warning that SharedArrayBuffer requires cross origin isolation.
      hasWasmThreads: false,
    };
  })();

  function errorHandler(message, filename, lineno) {
    // Unity needs to rely on Emscripten deferred fullscreen requests, so these will make their way to error handler
    if (message.indexOf('fullscreen error') != -1)
      return;

    if (Module.startupErrorHandler) {
      Module.startupErrorHandler(message, filename, lineno);
      return;
    }
    if (Module.errorHandler && Module.errorHandler(message, filename, lineno))
      return;
    console.log("Invoking error handler due to\n" + message);

    // Support Firefox window.dump functionality.
    if (typeof dump == "function")
      dump("Invoking error handler due to\n" + message);

    if (errorHandler.didShowErrorMessage)
      return;
    var message = "An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was:\n" + message;
    if (message.indexOf("DISABLE_EXCEPTION_CATCHING") != -1) {
      message = "An exception has occurred, but exception handling has been disabled in this build. If you are the developer of this content, enable exceptions in your project WebGL player settings to be able to catch the exception or see the stack trace.";
    } else if (message.indexOf("Cannot enlarge memory arrays") != -1) {
      message = "Out of memory. If you are the developer of this content, try allocating more memory to your WebGL build in the WebGL player settings.";
    } else if (message.indexOf("Invalid array buffer length") != -1  || message.indexOf("Invalid typed array length") != -1 || message.indexOf("out of memory") != -1 || message.indexOf("could not allocate memory") != -1) {
      message = "The browser could not allocate enough memory for the WebGL content. If you are the developer of this content, try allocating less memory to your WebGL build in the WebGL player settings.";
    }
    alert(message);
    errorHandler.didShowErrorMessage = true;
  }

#if SYMBOLS_FILENAME
  function demangleMessage(message, symbols) {
#if USE_WASM
    var symbolExp = "(wasm-function\\[)(\\d+)(\\])";
#else // USE_WASM
    var symbolExp = "(\\n|\\n    at |\\n    at Array\\.)([a-zA-Z0-9_$]+)(@| \\()";
#endif // USE_WASM
    var symbolRegExp = new RegExp(symbolExp);
    return message.replace(new RegExp(symbolExp, "g"), function (symbol) {
      var match = symbol.match(symbolRegExp);
#if USE_WASM
      return match[1] + (symbols[match[2]] ? symbols[match[2]] + "@" : "") + match[2] + match[3];
#else // USE_WASM
      return match[1] + match[2] + (symbols[match[2]] ? "[" + symbols[match[2]] + "]" : "") + match[3];
#endif // USE_WASM
    });
  }

  function demanglingErrorHandler(message, filename, lineno) {
    if (Module.symbols) {
      errorHandler(demangleMessage(message, Module.symbols), filename, lineno);
    } else if (!Module.symbolsUrl) {
      errorHandler(message, filename, lineno);
    } else {
      downloadBinary("symbolsUrl").then(function (data) {
        var json = "";
        for (var i = 0; i < data.length; i++)
          json += String.fromCharCode(data[i]);
        Module.symbols = JSON.parse(json);
        errorHandler(demangleMessage(message, Module.symbols), filename, lineno);
      }).catch(function (error) {
        errorHandler(message, filename, lineno);
      });
    }
  }

#endif // SYMBOLS_FILENAME

  Module.abortHandler = function (message) {
#if SYMBOLS_FILENAME
    demanglingErrorHandler(message, "", 0);
#else // SYMBOLS_FILENAME
    errorHandler(message, "", 0);
#endif // SYMBOLS_FILENAME
    return true;
  };

  Error.stackTraceLimit = Math.max(Error.stackTraceLimit || 0, 50);

  function progressUpdate(id, e) {
    if (id == "symbolsUrl")
      return;
    var progress = Module.downloadProgress[id];
    if (!progress)
      progress = Module.downloadProgress[id] = {
        started: false,
        finished: false,
        lengthComputable: false,
        total: 0,
        loaded: 0,
      };
    if (typeof e == "object" && (e.type == "progress" || e.type == "load")) {
      if (!progress.started) {
        progress.started = true;
        progress.lengthComputable = e.lengthComputable;
        progress.total = e.total;
      }
      progress.loaded = e.loaded;
      if (e.type == "load")
        progress.finished = true;
    }
    var loaded = 0, total = 0, started = 0, computable = 0, unfinishedNonComputable = 0;
    for (var id in Module.downloadProgress) {
      var progress = Module.downloadProgress[id];
      if (!progress.started)
        return 0;
      started++;
      if (progress.lengthComputable) {
        loaded += progress.loaded;
        total += progress.total;
        computable++;
      } else if (!progress.finished) {
        unfinishedNonComputable++;
      }
    }
    var totalProgress = started ? (started - unfinishedNonComputable - (total ? computable * (total - loaded) / total : 0)) / started : 0;
    onProgress(0.9 * totalProgress);
  }

#if USE_DATA_CACHING
  {{{ read("XMLHttpRequest.js") }}}
#endif // USE_DATA_CACHING

#if DECOMPRESSION_FALLBACK
  var decompressors = {
#if DECOMPRESSION_FALLBACK == "Gzip"
    gzip: {
      require: {{{ read("Gzip.js") }}},
      decompress: function (data) {
        if (!this.exports)
          this.exports = this.require("inflate.js");
        try { return this.exports.inflate(data) } catch (e) {};
      },
      hasUnityMarker: function (data) {
        var commentOffset = 10, expectedComment = "UnityWeb Compressed Content (gzip)";
        if (commentOffset > data.length || data[0] != 0x1F || data[1] != 0x8B)
          return false;
        var flags = data[3];
        if (flags & 0x04) {
          if (commentOffset + 2 > data.length)
            return false;
          commentOffset += 2 + data[commentOffset] + (data[commentOffset + 1] << 8);
          if (commentOffset > data.length)
            return false;
        }
        if (flags & 0x08) {
          while (commentOffset < data.length && data[commentOffset])
            commentOffset++;
          if (commentOffset + 1 > data.length)
            return false;
          commentOffset++;
        }
        return (flags & 0x10) && String.fromCharCode.apply(null, data.subarray(commentOffset, commentOffset + expectedComment.length + 1)) == expectedComment + "\0";
      },
    },
#endif // DECOMPRESSION_FALLBACK == "Gzip"
#if DECOMPRESSION_FALLBACK == "Brotli"
    br: {
      require: {{{ read("Brotli.js") }}},
      decompress: function (data) {
        if (!this.exports)
          this.exports = this.require("decompress.js");
        try { return this.exports(data) } catch (e) {};
      },
      hasUnityMarker: function (data) {
        var expectedComment = "UnityWeb Compressed Content (brotli)";
        if (!data.length)
          return false;
        var WBITS_length = (data[0] & 0x01) ? (data[0] & 0x0E) ? 4 : 7 : 1,
            WBITS = data[0] & ((1 << WBITS_length) - 1),
            MSKIPBYTES = 1 + ((Math.log(expectedComment.length - 1) / Math.log(2)) >> 3);
            commentOffset = (WBITS_length + 1 + 2 + 1 + 2 + (MSKIPBYTES << 3) + 7) >> 3;
        if (WBITS == 0x11 || commentOffset > data.length)
          return false;
        var expectedCommentPrefix = WBITS + (((3 << 1) + (MSKIPBYTES << 4) + ((expectedComment.length - 1) << 6)) << WBITS_length);
        for (var i = 0; i < commentOffset; i++, expectedCommentPrefix >>>= 8) {
          if (data[i] != (expectedCommentPrefix & 0xFF))
            return false;
        }
        return String.fromCharCode.apply(null, data.subarray(commentOffset, commentOffset + expectedComment.length)) == expectedComment;
      },
    },
#endif // DECOMPRESSION_FALLBACK == "Brotli"
  };

  function decompress(compressed, url, callback) {
    for (var contentEncoding in decompressors) {
      if (decompressors[contentEncoding].hasUnityMarker(compressed)) {
        if (url)
          console.log("You can reduce startup time if you configure your web server to add \"Content-Encoding: " + contentEncoding + "\" response header when serving \"" + url + "\" file.");
        var decompressor = decompressors[contentEncoding];
        if (!decompressor.worker) {
          var workerUrl = URL.createObjectURL(new Blob(["this.require = ", decompressor.require.toString(), "; this.decompress = ", decompressor.decompress.toString(), "; this.onmessage = ", function (e) {
            var data = { id: e.data.id, decompressed: this.decompress(e.data.compressed) };
            postMessage(data, data.decompressed ? [data.decompressed.buffer] : []);
          }.toString(), "; postMessage({ ready: true });"], { type: "application/javascript" }));
          decompressor.worker = new Worker(workerUrl);
          decompressor.worker.onmessage = function (e) {
            if (e.data.ready) {
              URL.revokeObjectURL(workerUrl);
              return;
            }
            this.callbacks[e.data.id](e.data.decompressed);
            delete this.callbacks[e.data.id];
          };
          decompressor.worker.callbacks = {};
          decompressor.worker.nextCallbackId = 0;
        }
        var id = decompressor.worker.nextCallbackId++;
        decompressor.worker.callbacks[id] = callback;
        decompressor.worker.postMessage({id: id, compressed: compressed}, [compressed.buffer]);
        return;
      }
    }
    callback(compressed);
  }
#endif // DECOMPRESSION_FALLBACK

  function downloadBinary(urlId) {
    return new Promise(function (resolve, reject) {
      progressUpdate(urlId);
#if USE_DATA_CACHING
      var xhr = Module.companyName && Module.productName ? new Module.XMLHttpRequest({
        companyName: Module.companyName,
        productName: Module.productName,
        cacheControl: Module.cacheControl(Module[urlId]),
      }) : new XMLHttpRequest();
#else // USE_DATA_CACHING
      var xhr = new XMLHttpRequest();
#endif // USE_DATA_CACHING
      xhr.open("GET", Module[urlId]);
      xhr.responseType = "arraybuffer";
      xhr.addEventListener("progress", function (e) {
        progressUpdate(urlId, e);
      });
      xhr.addEventListener("load", function(e) {
        progressUpdate(urlId, e);
#if DECOMPRESSION_FALLBACK
        decompress(new Uint8Array(xhr.response), Module[urlId], resolve);
#else // DECOMPRESSION_FALLBACK
        resolve(new Uint8Array(xhr.response));
#endif // DECOMPRESSION_FALLBACK
      });

      xhr.addEventListener("error", function(e) {
        var error = 'Failed to download file ' + Module[urlId]
        if (location.protocol == 'file:') {
          showBanner(error + '. Loading web pages via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host Unity content, or use the Unity Build and Run option.', 'error');
        } else {
          console.error(error);
        }
      });

      xhr.send();
    });
  }

  function downloadFramework() {
#if DECOMPRESSION_FALLBACK
    return downloadBinary("frameworkUrl").then(function (code) {
      var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
#if USE_THREADS
      Module.frameworkBlobUrl = blobUrl;
#endif // USE_THREADS
#endif // DECOMPRESSION_FALLBACK
      return new Promise(function (resolve, reject) {
        var script = document.createElement("script");
#if DECOMPRESSION_FALLBACK
        script.src = blobUrl;
#else // DECOMPRESSION_FALLBACK
        script.src = Module.frameworkUrl;
#endif // DECOMPRESSION_FALLBACK
        script.onload = function () {
          // Adding the framework.js script to DOM created a global
          // 'unityFramework' variable that should be considered internal.
          // If not, then we have received a malformed file.
          if (typeof unityFramework === 'undefined' || !unityFramework) {
            var compressions = [['br', 'br'], ['gz', 'gzip']];
            for(var i in compressions) {
              var compression = compressions[i];
              if (Module.frameworkUrl.endsWith('.' + compression[0])) {
                var error = 'Unable to parse ' + Module.frameworkUrl + '!';
                if (location.protocol == 'file:') {
                  showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
                  return;
                }
                error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
                if (compression[0] == 'br') {
                  if (location.protocol == 'http:') {
                    var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
                    if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
                    else error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
                  }
                }
                showBanner(error, 'error');
                return;
              }
            };
            showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
          }

          // Capture the variable to local scope and clear it from global
          // scope so that JS garbage collection can take place on
          // application quit.
          var fw = unityFramework;
          unityFramework = null;
          // Also ensure this function will not hold any JS scope
          // references to prevent JS garbage collection.
          script.onload = null;
#if DECOMPRESSION_FALLBACK && !USE_THREADS
          URL.revokeObjectURL(blobUrl);
#endif // DECOMPRESSION_FALLBACK && !USE_THREADS
          resolve(fw);
        }
        script.onerror = function(e) {
          showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
        }
        document.body.appendChild(script);
        Module.deinitializers.push(function() {
          document.body.removeChild(script);
        });
      });
#if DECOMPRESSION_FALLBACK
    });
#endif // DECOMPRESSION_FALLBACK
  }

#if !USE_WASM
  function downloadAsm() {
#if DECOMPRESSION_FALLBACK
    return downloadBinary("codeUrl").then(function (code) {
      var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
#endif // DECOMPRESSION_FALLBACK
      return new Promise(function (resolve, reject) {
        var script = document.createElement("script");
#if DECOMPRESSION_FALLBACK
        script.src = blobUrl;
#else // DECOMPRESSION_FALLBACK
        script.src = Module.codeUrl;
#endif // DECOMPRESSION_FALLBACK
#if USE_THREADS
        Module.asmJsUrlOrBlob = script.src;
#endif // USE_THREADS
        script.onload = function () {
          delete script.onload;
#if DECOMPRESSION_FALLBACK && !USE_THREADS
          URL.revokeObjectURL(blobUrl);
#endif // DECOMPRESSION_FALLBACK && !USE_THREADS
          resolve();
        }
        document.body.appendChild(script);
        Module.deinitializers.push(function() {
          document.body.removeChild(script);
        });
      });
#if DECOMPRESSION_FALLBACK
    });
#endif // DECOMPRESSION_FALLBACK
  }

#endif // !USE_WASM
  function loadBuild() {
#if USE_WASM
#if DECOMPRESSION_FALLBACK
    Promise.all([
      downloadFramework(),
      downloadBinary("codeUrl"),
    ]).then(function (results) {
      Module.wasmBinary = results[1];
      results[0](Module);
    });

#else // DECOMPRESSION_FALLBACK
    downloadFramework().then(function (unityFramework) {
      unityFramework(Module);
    });

#endif // DECOMPRESSION_FALLBACK
#else // USE_WASM
    Promise.all([
      downloadFramework(),
      downloadAsm(),
    ]).then(function (results) {
      results[0](Module);
    });

#endif // USE_WASM
#if MEMORY_FILENAME
    Module.memoryInitializerRequest = {
      addEventListener: function (type, listener) {
        if (type == "load")
          Module.memoryInitializerRequest.useRequest = listener;
      },
    };
    downloadBinary("memoryUrl").then(function (data) {
      Module.memoryInitializerRequest.status = 200;
      Module.memoryInitializerRequest.response = data;
      if (Module.memoryInitializerRequest.useRequest)
        Module.memoryInitializerRequest.useRequest();
    });

#endif // MEMORY_FILENAME
    var dataPromise = downloadBinary("dataUrl");
    Module.preRun.push(function () {
      Module.addRunDependency("dataUrl");
      dataPromise.then(function (data) {
        var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
        var pos = 0;
        var prefix = "UnityWebData1.0\0";
        if (!String.fromCharCode.apply(null, data.subarray(pos, pos + prefix.length)) == prefix)
          throw "unknown data format";
        pos += prefix.length;
        var headerSize = view.getUint32(pos, true); pos += 4;
        while (pos < headerSize) {
          var offset = view.getUint32(pos, true); pos += 4;
          var size = view.getUint32(pos, true); pos += 4;
          var pathLength = view.getUint32(pos, true); pos += 4;
          var path = String.fromCharCode.apply(null, data.subarray(pos, pos + pathLength)); pos += pathLength;
          for (var folder = 0, folderNext = path.indexOf("/", folder) + 1 ; folderNext > 0; folder = folderNext, folderNext = path.indexOf("/", folder) + 1)
            Module.FS_createPath(path.substring(0, folder), path.substring(folder, folderNext - 1), true, true);
          Module.FS_createDataFile(path, null, data.subarray(offset, offset + size), true, true, true);
        }
        Module.removeRunDependency("dataUrl");
      });
    });
  }

  return new Promise(function (resolve, reject) {
    if (!Module.SystemInfo.hasWebGL) {
      reject("Your browser does not support WebGL.");
  #if !USE_WEBGL_1_0
    } else if (Module.SystemInfo.hasWebGL == 1) {
      reject("Your browser does not support graphics API \"WebGL 2\" which is required for this content.");
  #endif // !USE_WEBGL_1_0
  #if USE_WASM
    } else if (!Module.SystemInfo.hasWasm) {
      reject("Your browser does not support WebAssembly.");
  #endif // USE_WASM
  #if USE_THREADS
    } else if (!Module.SystemInfo.hasThreads) {
      reject("Your browser does not support multithreading.");
  #endif // USE_THREADS
    } else {
  #if USE_WEBGL_2_0
      if (Module.SystemInfo.hasWebGL == 1)
        Module.print("Warning: Your browser does not support \"WebGL 2\" Graphics API, switching to \"WebGL 1\"");
  #endif // USE_WEBGL_2_0
      Module.startupErrorHandler = reject;
      onProgress(0);
      Module.postRun.push(function () {
        onProgress(1);
        delete Module.startupErrorHandler;
        resolve(unityInstance);
      });
      loadBuild();
    }
  });
}

Can be usefull

My solution (for now) is to rewrite some UnityLoader.js logic, namely remove the #if DECOMPRESSION_FALLBACK checks in the methods corresponding to loading the relevant files.

As it was already described above, when the Decompression Fallback parameter was turned on, caching in IndexedDB worked. That’s why I turned it off and removed checks on it where I “don’t need it”. Tested and did not notice that my changes created other problems.

Still wondering if it is possible to cache not the .wasm and .asm.js files, but rather their “compiled output”, if there is such a thing and it is possible to implement.

  // function downloadFramework() {
  //   #if DECOMPRESSION_FALLBACK
  //       return downloadBinary("frameworkUrl").then(function (code) {
  //         var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
  //   #if USE_THREADS
  //         Module.frameworkBlobUrl = blobUrl;
  //   #endif // USE_THREADS
  //   #endif // DECOMPRESSION_FALLBACK
  //         return new Promise(function (resolve, reject) {
  //           var script = document.createElement("script");
  //   #if DECOMPRESSION_FALLBACK
  //           script.src = blobUrl;
  //   #else // DECOMPRESSION_FALLBACK
  //           script.src = Module.frameworkUrl;
  //   #endif // DECOMPRESSION_FALLBACK
  //           script.onload = function () {
  //             // Adding the framework.js script to DOM created a global
  //             // 'unityFramework' variable that should be considered internal.
  //             // If not, then we have received a malformed file.
  //             if (typeof unityFramework === 'undefined' || !unityFramework) {
  //               var compressions = [['br', 'br'], ['gz', 'gzip']];
  //               for(var i in compressions) {
  //                 var compression = compressions[i];
  //                 if (Module.frameworkUrl.endsWith('.' + compression[0])) {
  //                   var error = 'Unable to parse ' + Module.frameworkUrl + '!';
  //                   if (location.protocol == 'file:') {
  //                     showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
  //                     return;
  //                   }
  //                   error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
  //                   if (compression[0] == 'br') {
  //                     if (location.protocol == 'http:') {
  //                       var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
  //                       if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
  //                       else error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
  //                     }
  //                   }
  //                   showBanner(error, 'error');
  //                   return;
  //                 }
  //               };
  //               showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
  //             }
   
  //             // Capture the variable to local scope and clear it from global
  //             // scope so that JS garbage collection can take place on
  //             // application quit.
  //             var fw = unityFramework;
  //             unityFramework = null;
  //             // Also ensure this function will not hold any JS scope
  //             // references to prevent JS garbage collection.
  //             script.onload = null;
  //   #if DECOMPRESSION_FALLBACK && !USE_THREADS
  //             URL.revokeObjectURL(blobUrl);
  //   #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
  //             resolve(fw);
  //           }
  //           script.onerror = function(e) {
  //             showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
  //           }
  //           document.body.appendChild(script);
  //           Module.deinitializers.push(function() {
  //             document.body.removeChild(script);
  //           });
  //         });
  //   #if DECOMPRESSION_FALLBACK
  //       });
  //   #endif // DECOMPRESSION_FALLBACK
  //     }

  function downloadFramework() {
    return downloadBinary("frameworkUrl").then(function (code) {
      var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
#if USE_THREADS
      Module.frameworkBlobUrl = blobUrl;
#endif // USE_THREADS
      return new Promise(function (resolve, reject) {
        var script = document.createElement("script");
        script.src = blobUrl;
        script.onload = function () {
          // Adding the framework.js script to DOM created a global
          // 'unityFramework' variable that should be considered internal.
          // If not, then we have received a malformed file.
          if (typeof unityFramework === 'undefined' || !unityFramework) {
            var compressions = [['br', 'br'], ['gz', 'gzip']];
            for(var i in compressions) {
              var compression = compressions[i];
              if (Module.frameworkUrl.endsWith('.' + compression[0])) {
                var error = 'Unable to parse ' + Module.frameworkUrl + '!';
                if (location.protocol == 'file:') {
                  showBanner(error + ' Loading pre-compressed (brotli or gzip) content via a file:// URL without a web server is not supported by this browser. Please use a local development web server to host compressed Unity content, or use the Unity Build and Run option.', 'error');
                  return;
                }
                error += ' This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header "Content-Encoding: ' + compression[1] + '" present. Check browser Console and Devtools Network tab to debug.';
                if (compression[0] == 'br') {
                  if (location.protocol == 'http:') {
                    var migrationHelp = ['localhost', '127.0.0.1'].indexOf(location.hostname) != -1 ? '' : 'Migrate your server to use HTTPS.'
                    if (/Firefox/.test(navigator.userAgent)) error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported in Firefox over HTTP connections. ' + migrationHelp + ' See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1670675">https://bugzilla.mozilla.org/show_bug.cgi?id=1670675</a> for more information.';
                    else error = 'Unable to parse ' + Module.frameworkUrl + '!
If using custom web server, verify that web server is sending .br files with HTTP Response Header "Content-Encoding: br". Brotli compression may not be supported over HTTP connections. Migrate your server to use HTTPS.';
                  }
                }
                showBanner(error, 'error');
                return;
              }
            };
            showBanner('Unable to parse ' + Module.frameworkUrl + '! The file is corrupt, or compression was misconfigured? (check Content-Encoding HTTP Response Header on web server)', 'error');
          }

          // Capture the variable to local scope and clear it from global
          // scope so that JS garbage collection can take place on
          // application quit.
          var fw = unityFramework;
          unityFramework = null;
          // Also ensure this function will not hold any JS scope
          // references to prevent JS garbage collection.
          script.onload = null;
#if !USE_THREADS
          URL.revokeObjectURL(blobUrl);
#endif // !USE_THREADS
          resolve(fw);
        }
        script.onerror = function(e) {
          showBanner('Unable to load file ' + Module.frameworkUrl + '! Check that the file exists on the remote server. (also check browser Console and Devtools Network tab to debug)', 'error');
        }
        document.body.appendChild(script);
        Module.deinitializers.push(function() {
          document.body.removeChild(script);
        });
      });
    });
  }

// #if !USE_WASM
//   function downloadAsm() {
// #if DECOMPRESSION_FALLBACK
//     return downloadBinary("codeUrl").then(function (code) {
//       var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
// #endif // DECOMPRESSION_FALLBACK
//       return new Promise(function (resolve, reject) {
//         var script = document.createElement("script");
// #if DECOMPRESSION_FALLBACK
//         script.src = blobUrl;
// #else // DECOMPRESSION_FALLBACK
//         script.src = Module.codeUrl;
// #endif // DECOMPRESSION_FALLBACK
// #if USE_THREADS
//         Module.asmJsUrlOrBlob = script.src;
// #endif // USE_THREADS
//         script.onload = function () {
//           delete script.onload;
// #if DECOMPRESSION_FALLBACK && !USE_THREADS
//           URL.revokeObjectURL(blobUrl);
// #endif // DECOMPRESSION_FALLBACK && !USE_THREADS
//           resolve();
//         }
//         document.body.appendChild(script);
//         Module.deinitializers.push(function() {
//           document.body.removeChild(script);
//         });
//       });
// #if DECOMPRESSION_FALLBACK
//     });
// #endif // DECOMPRESSION_FALLBACK
//   }
// #endif // !USE_WASM


#if !USE_WASM
  function downloadAsm() {
    return downloadBinary("codeUrl").then(function (code) {
      var blobUrl = URL.createObjectURL(new Blob([code], { type: "application/javascript" }));
      return new Promise(function (resolve, reject) {
        var script = document.createElement("script");
        script.src = blobUrl;
#if USE_THREADS
        Module.asmJsUrlOrBlob = script.src;
#endif // USE_THREADS
        script.onload = function () {
          delete script.onload;
#if !USE_THREADS
          URL.revokeObjectURL(blobUrl);
#endif // !USE_THREADS // DECOMPRESSION_FALLBACK && !USE_THREADS
          resolve();
        }
        document.body.appendChild(script);
        Module.deinitializers.push(function() {
          document.body.removeChild(script);
        });
      });
    });
  }
#endif // !USE_WASM


  function loadBuild() {
    console.log("loadBuild()");
#if USE_WASM
// #if DECOMPRESSION_FALLBACK
//     Promise.all([
//       downloadFramework(),
//       downloadBinary("codeUrl"),
//     ]).then(function (results) {
//       Module.wasmBinary = results[1];
//       results[0](Module);
//     });

// #else // DECOMPRESSION_FALLBACK
//     downloadFramework().then(function (unityFramework) {
//       unityFramework(Module);
//     });

// #endif // DECOMPRESSION_FALLBACK
    Promise.all([
      downloadFramework(),
      downloadBinary("codeUrl"),
    ]).then(function (results) {
      Module.wasmBinary = results[1];
      results[0](Module);
    });
#else // USE_WASM
    Promise.all([
      downloadFramework(),
      downloadAsm(),
    ]).then(function (results) {
      results[0](Module);
    });
#endif // USE_WASM

We have been looking into the lack of caching for .wasm files as well. Is this last post of yours a functional solution? or are you just following the thread you are on.

1 Like

Not as much as I hoped, because the first run does not use instantiateStreaming (so it turns out that I actually enabled Decompression Fallback without enabling it)

Therefore, now the hope is to disable this parameter and process (rewrite the logic) precisely at the level of the integrateWasmJS() method

This is all relevant only when you cannot configure headers on the server or native caching is not available due to the specifics of the platform (as in my case - clearing the cache at each individual launch of the web application on the TV).

So if your goal is not as specific as mine - you just need to configure the headers for the files, depending on where you host the files, it can be different, in my case I use https://www.netlify.com/ and add rules for headers using the netlify.toml file with the content below.

Cache-Control = “public, max-age=31536000” - for browser caching (that is what your want)

Please note that the last rules cannot be universal for the option when you use Decompression Fallback - because the extensions (.unityweb) for gzip and br are the same - so change the Content-Encoding for them on the server according to your settings.

If it works, you’ll see “cached from disk” next to your files in the Network panel of dev browser tools (F12).

[[headers]]
  for = "/*.mem"
  [headers.values]
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.mem.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.mem.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.data"
  [headers.values]
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.data.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.data.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.symbols.json.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/octet-stream"

[[headers]]
  for = "/*.symbols.json.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"

[[headers]]
  for = "/*.js.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/javascript"

[[headers]]
  for = "/*.js.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/javascript"

[[headers]]
  for = "/*.framework.js.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/javascript"

[[headers]]
  for = "/*.framework.js.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/javascript"

[[headers]]
  for = "/*.asm.js"
  [headers.values]
    Content-Type = "application/javascript"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.asm.js.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/javascript"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.asm.js.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/javascript"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.wasm"
  [headers.values]
    Content-Type = "application/wasm"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.wasm.br"
  [headers.values]
    Content-Encoding = "br"
    Content-Type = "application/wasm"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.wasm.gz"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/wasm"
    Cache-Control = "public, max-age=31536000"



[[headers]]
  for = "/*.wasm.unityweb"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/wasm"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.data.unityweb"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.js.unityweb"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/javascript"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.mem.unityweb"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"
    Cache-Control = "public, max-age=31536000"

[[headers]]
  for = "/*.symbols.json.unityweb"
  [headers.values]
    Content-Encoding = "gzip"
    Content-Type = "application/octet-stream"
1 Like