Skip to content

fix: re-inject user data when widget signals it is ready#59

Open
alperbilgic wants to merge 1 commit intochatwoot:developfrom
alperbilgic:fix/reinject-user-data-on-widget-loaded
Open

fix: re-inject user data when widget signals it is ready#59
alperbilgic wants to merge 1 commit intochatwoot:developfrom
alperbilgic:fix/reinject-user-data-on-widget-loaded

Conversation

@alperbilgic
Copy link

@alperbilgic alperbilgic commented Feb 17, 2026

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

injectedJavaScript 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. The window.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 loaded event — the exact moment it signals readiness to receive messages.

  • The existing injectedJavaScript prop is kept as an optimistic first attempt (works when timing is favorable)
  • The loaded event handler acts as a reliable, event-driven fallback

This is a minimal, non-breaking change — only src/WebView.js is touched.

Changes

  1. Import useRef from React
  2. Create a webViewRef and attach it to the <WebView> component
  3. In the onMessage handler, when eventType === 'loaded', call webViewRef.current.injectJavaScript() with the same scripts

Related issues

Fixes #31

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set user info not working

1 participant