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 <noreply@anthropic.com>

* docs: Add changelog entry for #4872

---------

Co-authored-by: ddmoney420 <ddmoney420@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
This commit is contained in:
ddmoney420 2026-02-02 02:00:18 -07:00 committed by GitHub
commit 5d0dd30685
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)
}