From 5d0dd306857c6af768e3406a1e1ee54efb13b033 Mon Sep 17 00:00:00 2001 From: ddmoney420 <130018552+ddmoney420@users.noreply.github.com> Date: Mon, 2 Feb 2026 02:00:18 -0700 Subject: [PATCH] fix(v3): guard dispatchWailsEvent against race condition on reload (#4925) * fix(v3): guard dispatchWailsEvent against race condition on reload When the page is reloaded, the WindowLoadFinished event can fire before the JavaScript runtime has mounted dispatchWailsEvent on window._wails. This causes a TypeError: window._wails.dispatchWailsEvent is not a function. This fix adds a guard to check if window._wails and dispatchWailsEvent exist before attempting to call the function. If the runtime isn't ready, the event is silently skipped (which is correct since there's no handler). Fixes #4872 Co-Authored-By: Claude Opus 4.5 * docs: Add changelog entry for #4872 --------- Co-authored-by: ddmoney420 Co-authored-by: Claude Opus 4.5 Co-authored-by: Lea Anthony --- v3/pkg/application/webview_window.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index 38ead6806..bad4058b3 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -1180,7 +1180,10 @@ func (w *WebviewWindow) SetFrameless(frameless bool) Window { } func (w *WebviewWindow) DispatchWailsEvent(event *CustomEvent) { - msg := fmt.Sprintf("window._wails.dispatchWailsEvent(%s);", event.ToJSON()) + // Guard against race condition where event fires before runtime is initialized + // This can happen during page reload when WindowLoadFinished fires before + // the JavaScript runtime has mounted dispatchWailsEvent on window._wails + msg := fmt.Sprintf("if(window._wails&&window._wails.dispatchWailsEvent){window._wails.dispatchWailsEvent(%s);}", event.ToJSON()) w.ExecJS(msg) }