Custom JS for Lizmap fails on Windows 11 Systems, Problem Solving recommendations

Hi all,

I’d like to share a problem I ran into with custom JavaScript files in Lizmap and a pattern that makes them more robust. Maybe it helps others, and maybe someone can say more about the root cause.

Setup

  • Lizmap Web Client [Lizmap 3.10.0-rc.4-pre.11902], project configured with the Lizmap plugin targeting 3.10
  • Several custom JS files in media/js/<project>/ (home button redirect, external links dock, Google Street View, theme name display)
  • Most of them follow the common pattern from the Lizmap JS examples:

js

lizMap.events.on({
  uicreated: function () {
    // add dock, bind click handlers, ...
  }
});

Symptom

On macOS everything worked. On Windows 11 with Microsoft Edge, none of the custom features appeared. For example, the Home icon opened the Lizmap project list instead of our website. At first I suspected that Edge was not executing the JS. That was wrong: all project JS files were loaded correctly.

Analysis

The browser console showed this error during map initialization:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'w')
    at initialize.setMap (assets/js/OpenLayers-2.13/OpenLayers.js)
    at initialize.addLayer (assets/js/OpenLayers-2.13/OpenLayers.js)
    at Object.completeInitialization (assets/js/map.js)
    at assets/js/lizmap.js

My understanding of what happens:

  1. Lizmap initializes the map in a promise chain: load config, create the map, add layers (completeInitialization), and only at the end trigger uicreated.
  2. When a layer is added, OpenLayers 2 calls map.getSize().w. getSize() returns null if the map container has no measurable size at that moment, for example because layout or CSS is not finished yet, or the tab is in the background.
  3. The exception aborts the promise chain, so uicreated is never triggered.
  4. Every custom script that waits for uicreated silently does nothing.

Interestingly, the map itself was still displayed and usable. Only the legacy OpenLayers 2 part failed. So the custom scripts could have worked; they just never got the signal to start.

Whether the container has a size in time seems to depend on timing (browser engine, cache state, window size, display scaling). That would explain why it worked on one machine and not on another. I could reproduce the same error in a Chromium-based browser on macOS while the window was in the background.

Robust pattern

Instead of relying only on uicreated, I now start each script through a small helper. It runs the callback exactly once: on uicreated in the normal case, or as a fallback as soon as the map and the menu exist, if initialization aborted with an uncaught promise error or uicreated did not arrive within 10 seconds.

js

window.onLizmapReady = window.onLizmapReady || function (fn) {
    var done = false;
    var polling = false;
    function run() {
        if (done) { return; }
        done = true;
        try { fn(); } catch (e) { console.error('onLizmapReady callback failed:', e); }
    }
    function isReady() {
        return !!(typeof lizMap !== 'undefined' && lizMap.mainLizmap &&
                  lizMap.mainLizmap.map && document.getElementById('mapmenu'));
    }
    function startFallback() {
        if (done || polling) { return; }
        polling = true;
        var tries = 0;
        var timer = setInterval(function () {
            if (done || ++tries > 120) { clearInterval(timer); return; }
            if (isReady()) { clearInterval(timer); run(); }
        }, 250);
    }
    // Normal case
    lizMap.events.on({ uicreated: run });
    // Fallback 1: map initialization aborted with an uncaught promise error
    window.addEventListener('unhandledrejection', function () { setTimeout(startFallback, 500); });
    // Fallback 2: safety net if 'uicreated' never arrives
    setTimeout(startFallback, 10000);
};

Usage: replace

js

lizMap.events.on({ uicreated: function () { ... } });

with

js

onLizmapReady(function () { ... });

Other events like dockopened or minidockclosed can stay as they are.

The helper is defined with window.onLizmapReady = window.onLizmapReady || ..., so each JS file can carry its own copy and still work independently, regardless of load order.

Simpler cases need no Lizmap event at all

For a Home button redirect, a delegated click handler on document level is enough. It works immediately:

js

(function () {
  var target = 'https://www.example.org/';
  var sel = '.nav-list .home a';
  $(document).on('click', sel, function (e) {
    e.preventDefault();
    window.location.href = target;
  });
})();

To hide a menu entry, an injected CSS rule works without any event:

js

(function () {
  var style = document.createElement('style');
  style.appendChild(document.createTextNode('.nav-list .home { display: none !important; }'));
  document.head.appendChild(style);
})();

Additional notes

  • If a script may run twice (for example uicreated plus a fallback, or the Google Maps API callback), add a guard against double initialization, e.g. check whether its dock element already exists.
  • Scripts that read the projection from lizMap.map.projection.projCode depend on the legacy OpenLayers 2 map. lizMap.mainLizmap.projection is available on the current map.

Open questions

  • Is this getSize() null error during completeInitialization a known issue, especially with 3.10?
  • Is there an official event or recommended way for custom JS that should not depend on a successful map initialization?
  • The pattern makes custom scripts robust, but it does not fix the initialization error itself.

Thanks for any feedback!

This problem was already discussed on github, see:

Maybe this helps? I do not have much JS or any problems with it, so i’m not too heavily invested into that topic! :smiley: