Skip to content

Commit 1730675

Browse files
committed
fix(example): resolve race between async Maps callback and deferred ESM module
Google Maps loaded with async+callback could fire before the type=module script assigned window.initApp, leaving the map blank. Stub initApp as a promise resolver synchronously, await it inside the module.
1 parent 06ffb4f commit 1730675

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

example/index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ <h2>Presets</h2>
396396
</div>
397397
</footer>
398398

399+
<!-- Bridge: Maps' async callback resolves a promise the module awaits. -->
400+
<script>
401+
window.__mapsReady = new Promise((resolve) => {
402+
window.initApp = resolve;
403+
});
404+
</script>
405+
399406
<!-- Google Maps JS API. Replace YOUR_API_KEY. -->
400407
<script
401408
async
@@ -414,7 +421,8 @@ <h2>Presets</h2>
414421
el.className = 'status' + (cls ? ' ' + cls : '');
415422
}
416423

417-
window.initApp = function () {
424+
await window.__mapsReady;
425+
(function init() {
418426
const latView = $('latView');
419427
const lngView = $('lngView');
420428

@@ -493,7 +501,7 @@ <h2>Presets</h2>
493501
lp.setLocation(lat, lng);
494502
updateView({ lat, lng });
495503
});
496-
};
504+
})();
497505
</script>
498506
</body>
499507
</html>

0 commit comments

Comments
 (0)