fix: re-inject user data when widget signals it is ready#59
Open
alperbilgic wants to merge 1 commit intochatwoot:developfrom
Open
fix: re-inject user data when widget signals it is ready#59alperbilgic wants to merge 1 commit intochatwoot:developfrom
alperbilgic wants to merge 1 commit intochatwoot:developfrom
Conversation
The `injectedJavaScript` prop fires as soon as the WebView page finishes loading, but the Chatwoot widget's internal JavaScript may not have initialized its message event listeners at that point. This causes a race condition where user data (name, email, identifier), locale, custom attributes, and color scheme sent via `window.postMessage()` are silently dropped. Fix this by adding a WebView ref and re-injecting the same scripts when the widget fires its `loaded` event — the exact moment the widget is ready to receive messages. The `injectedJavaScript` prop is kept as an optimistic first attempt (it works when timing is favorable), while the `loaded` handler acts as a reliable, event-driven fallback. Fixes chatwoot#31 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
User data (name, email, identifier), locale, custom attributes, and color scheme are intermittently not applied to the Chatwoot widget. This is a race condition reported in #31.
Root cause
injectedJavaScriptfires as soon as the WebView page finishes loading, but the Chatwoot widget's internal JavaScript may not have initialized itsmessageevent listeners at that point. Thewindow.postMessage()calls containing user data are silently dropped.Solution
Add a WebView ref and re-inject the user/locale/customAttributes/colorScheme scripts when the widget fires its
loadedevent — the exact moment it signals readiness to receive messages.injectedJavaScriptprop is kept as an optimistic first attempt (works when timing is favorable)loadedevent handler acts as a reliable, event-driven fallbackThis is a minimal, non-breaking change — only
src/WebView.jsis touched.Changes
useReffrom ReactwebViewRefand attach it to the<WebView>componentonMessagehandler, wheneventType === 'loaded', callwebViewRef.current.injectJavaScript()with the same scriptsRelated issues
Fixes #31