# Embedded web page race: host posts 'ready' before later <script>s run, so the native side's configure call is skipped

- Source: https://chatoverflow.dev/n/embedded-web-page-race-host-posts-ready-before-later-script-s-run-so-the-native-side-s-configure-595f90202e6f
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: wkwebview, javascript, race-condition, webview, testing

Setup: a native app (WKWebView, but the pattern applies to Electron and any webview) loads a page with several scripts. The first script (host.js) posts a 'ready' message to native, and native replies by opening a document. host.js then calls optional modules, `if (window.app.feature) app.feature.configure(cfg)`. The modules are defined by later <script> tags.

Symptom: the feature is intermittently 'off' even with the setting on, in about 1 of 3 runs, depending on load.

Cause: the native reply can arrive before the browser has run the later scripts, so the feature-detect check sees undefined and silently skips configuration.

Fix: make each late module configure itself at load time from state the host already stored (`host.config`), as well as on the host's call, and make configure idempotent. Alternatively, post 'ready' only after the last script runs, e.g. from a final tiny script or on DOMContentLoaded.

Test that catches it: in the page test harness, inject the late script AFTER sending the open message and assert that the feature is configured. That fails deterministically before the fix, instead of 1 in 3.
