Skip to content

fix(widget): persist pre-chat userData and send with every request (#113) - #155

Merged
anurag629 merged 2 commits into
codercops:devfrom
bhushan4work:fix#113
Oct 19, 2026
Merged

anurag629 merged 2 commits into
codercops:devfrom
bhushan4work:fix#113

Conversation

@bhushan4work

Copy link
Copy Markdown
Contributor

What does this change?

Persist pre-chat answers in sessionStorage, restore them when the widget is recreated in the same tab, and send userData with every request so visitor context is preserved throughout the conversation.

Closes #113

How I tested this

  • Added widget tests covering subsequent requests and widget recreation.
  • pnpm -r build
  • pnpm test
  • pnpm -r typecheck

AI assistance

none

Checklist

  • This PR targets dev
  • I was assigned the linked issue
  • pnpm -r build, pnpm test and pnpm -r typecheck pass locally
  • Tests added or updated where it makes sense
  • For a bug fix: the new test fails without my change
  • Changeset added (pnpm changeset) if this changes core, widget or server
  • Docs updated if behavior or options changed

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bhushan4work, that was quick. It works: the answers come back after destroy() and a new Widget, every request carries them, nothing is sent with the form off, and five of the seven new tests fail on dev's code. A few things to tidy before I merge:

  • getPreChatData also reads chatcops-prechat-userdata-<id>, but nothing has ever written that key, so the fallback can go.
  • getPreChatUserData and setPreChatUserData only forward to getPreChatData and setPreChatData, and only the tests call them. Please drop them.
  • The issue asks for a test that the next request after destroy() and a new Widget still carries userData. The recreation test only checks the greeting. One handleSend on widget2 with the same assertion as in your other test covers it (I tried it, and it passes).
  • tests/setup.ts swaps out storage for every widget test just so the failure tests can replace setItem and getItem. vi.spyOn(sessionStorage, 'setItem').mockImplementation(...) works with happy-dom's storage as it is, so the setup file and the vite.config.ts change can go too.

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bhushan4work, all four are done. On top of current dev the build, typecheck and all 272 tests pass, five of the new tests fail with dev's widget code, and the recreation test now checks the request too. I updated the branch with dev so it could merge. Merging.

@anurag629 anurag629 added the hacktoberfest-accepted PR counts for Hacktoberfest label Oct 19, 2026
@anurag629
anurag629 merged commit 2227526 into codercops:dev Oct 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest-accepted PR counts for Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep pre-chat answers after the first reply and across page loads

2 participants