Skip to content

Add onAfterSubmit timing demo for the feedback widget - #800

Open
tinsmith-dev[bot] wants to merge 3 commits into
mainfrom
tin/feedback-after-submit-demo
Open

tinsmith-dev[bot] wants to merge 3 commits into
mainfrom
tin/feedback-after-submit-demo

Conversation

@tinsmith-dev

@tinsmith-dev tinsmith-dev Bot commented Nov 8, 2026

Copy link
Copy Markdown

Why

#798 raised a concern worth settling with evidence rather than argument: onAfterSubmit only fires after the feedback POST resolves, so there's a real window where closing the tab (or navigating away) could lose the callback. This adds a runnable demo that makes that window visible and then proves what actually happens to it.

What's in it

packages/browser-sdk/example/feedback-after-submit-demo/ — a small app that opens the real feedback widget (built from this branch's src/, not a mock) against a local mock API with configurable artificial latency:

  1. Artificial latency control — widens the gap between submit and onAfterSubmit so it's easy to watch.
  2. Live widget — a banner stays red for as long as a feedback POST is pending, turns amber once the response is back but onAfterSubmit is still waiting on Send/close (by design), and green once the callback actually runs. A log shows exact timestamps for each step.
  3. Danger zone — fires the same feedback() call the widget's Send button makes, then navigates away before the response can arrive, simulating a closed tab. A persisted table cross-references the mock server's own receipt log (ground truth, independent of the tab) against whether any onAfterSubmit-equivalent callback ran client-side.

Confirmed findings from running it:

  • onAfterSubmit never fires once the tab is gone — guaranteed, every time, regardless of network speed. There's no way around this: the page that would run the callback no longer exists.
  • Separately, the SDK's feedback POST isn't sent with keepalive, so on a slower connection than localhost, the data itself (not just the callback) can be lost outright. The demo's keepalive checkbox (bypassing the SDK, since it doesn't expose that option) shows what addressing that half would look like.

See the demo's own README for how to run it and what to look at.

Notes for the reviewer

This only adds a new example directory — no changes to src/. It's independent of #798 (built here against current main); the same gap exists either way since #798 doesn't change the fundamental await-then-notify timing, only when the callback fires exactly once.

🤖 Generated with Claude Code

Adds a runnable demo that opens the real feedback widget against a local
mock API with configurable latency, to make visible the gap between a
user submitting feedback and onAfterSubmit firing. A "navigate away
mid-submit" scenario proves the callback never survives a closed tab,
and that the SDK's feedback POST isn't sent with `keepalive`.
- gone.html always redirects back to its own index page instead of a
  caller-supplied "return" URL, closing an open-redirect/XSS sink.
- server.mjs validates the requested delay with a strict range check
  instead of clamping, so CodeQL can see the timer duration is bounded.
Formatting-only (line wrap, import order); no behavior change.

@reflag-review reflag-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Risk: medium

2 actionable findings; 1 optional refactor suggestion.

}

if (req.method === "POST" && url.pathname === "/feedback") {
const body = await readBody(req);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Catch request-body failures before they terminate the demo

If a request is aborted while its body is being read, or contains malformed JSON, readBody rejects. The async HTTP listener does not catch that rejection, and Node's HTTP server does not await listener promises. Under Node's default unhandled-rejection behavior, this terminates the mock API; start.mjs then shuts down Vite too. Interrupted uploads are particularly relevant to this demo's navigation experiment. Catch body-reading failures in the request handler, return a 400 response when possible, and tolerate disconnected clients without terminating the server.

const feedbackId = randomUUID();
const attemptMatch = ATTEMPT_RE.exec(body.comment ?? "");

state.received.push({

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Record receipt before delaying the response

The request body has already been fully received at line 57, but it is not added to the receipt log until after the artificial delay. With a supported delay such as 20,000 ms, the browser returns from gone.html after roughly three seconds and repeatedly reports “feedback lost entirely” for a request the server already received, until the delay expires. This makes the demo's advertised ground-truth column conflate receipt with response completion. Add the receipt entry immediately after reading the body, before awaiting the delay, and update respondedAt separately when sending the response.

: `⚠ response received ${elapsed}ms ago, feedback is saved server-side — but onAfterSubmit is still waiting on Send or the dialog closing. Closing the tab right now would lose the callback (not the data).`;
}

function closeRiskWindow(fired: boolean) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Refactor suggestion — optional] Remove the unused false mode from closeRiskWindow

closeRiskWindow has a single caller, in onAfterSubmit, which always passes true. Remove the fired parameter, unwrap the if (fired) block, and call closeRiskWindow() instead. Keeping the existing timer cleanup and pendingSince guard preserves behavior while removing an unused mode from the banner lifecycle.

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.

2 participants