Repository navigation
Add onAfterSubmit timing demo for the feedback widget - #800
tinsmith-dev[bot] wants to merge 3 commits into
Conversation
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.
| } | ||
|
|
||
| if (req.method === "POST" && url.pathname === "/feedback") { | ||
| const body = await readBody(req); |
There was a problem hiding this comment.
[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({ |
There was a problem hiding this comment.
[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) { |
There was a problem hiding this comment.
[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.
Why
#798 raised a concern worth settling with evidence rather than argument:
onAfterSubmitonly 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'ssrc/, not a mock) against a local mock API with configurable artificial latency:onAfterSubmitso it's easy to watch.onAfterSubmitis still waiting on Send/close (by design), and green once the callback actually runs. A log shows exact timestamps for each step.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 anyonAfterSubmit-equivalent callback ran client-side.Confirmed findings from running it:
onAfterSubmitnever 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.keepalive, so on a slower connection than localhost, the data itself (not just the callback) can be lost outright. The demo'skeepalivecheckbox (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 currentmain); 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