[Fixed] Error in Edge browser: TypeError: Cannot destructure property 'base64DecToArr' of 'window.b64' as it is undefined

What I’m trying to do:
anvil.users raises error as part of my app startup. Everything works fine in Chrome (Windows) and Safari (mobile).

This issue only occurs on a custom domain. When I create a custom environment using the default private/public anvil.app URL, the error does not occur.

What I’ve tried and what’s not working:

  • The error only happens in Edge (Windows).
  • I think the error first appeared on Monday.
  • Clearing cache/restarting the browser did not help.

Error message:

ExternalError: TypeError: Cannot destructure property 'base64DecToArr' of 'window.b64' as it is undefined.
at anvil-services/anvil/users/mfa/__init__.py, line 51
called from anvil-services/anvil/users/__init__.py, line 78
called from Startup, line 2

Code Sample:

from anvil import *
import anvil.users  # Error occurs here
import anvil.microsoft.auth
import anvil.server
from routing.router import launch
from . import Routes  # noqa: F401

from .Modules.Users import UserItem
from .Modules.AvvikDepartmentResponsible import AvvikDepartmentResponsibleItem
from .Modules.AvvikUserInfo import AvvikUserInfoItem

if __name__ == "__main__":
  launch()

Workaround that stopped the error

With some help from ChatGPT, I added this polyfill at app startup. After adding this, the error disappeared and anvil.users started working normally again.

Hope it is safe :slight_smile:

from anvil.js import window

# Define the polyfill first
window.eval("""
(function () {
  if (!window.b64) {
    function base64DecToArr (b64) {
      var bin = atob(b64);
      var len = bin.length, out = new Uint8Array(len);
      for (var i = 0; i < len; i++) out[i] = bin.charCodeAt(i);
      return out;
    }
    function base64EncArr (bytes) {
      var bin = '';
      for (var i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
      return btoa(bin);
    }
    window.b64 = { base64DecToArr, base64EncArr };
  }
})();
""")
1 Like

thanks we’ll get that fixed

1 Like