Skip to content

Prefer cached play over stale db play for signed-in users - #610

Merged
mdirolf merged 1 commit into
crosshare-org:masterfrom
macgyver:better-progress-capture-for-authenticated-users
Nov 13, 2026
Merged

mdirolf merged 1 commit into
crosshare-org:masterfrom
macgyver:better-progress-capture-for-authenticated-users

Conversation

@macgyver

Copy link
Copy Markdown
Contributor

The db write on page exit isn't reliable (browsers abandon the queued request before it can complete), so on reload the db play could be behind (or missing) while local storage had the latest progress. Use whichever is fresher and write the cached play back to the db on load.

This fixes lost progress in a single browser for signed-in users, but a lost exit save still strands progress on that device (so users moving to a new device/browser will not see their progress maintained). Two small follow-ups would close most of that gap:

  1. Debounced db saves during play (maybe 15–30s after the last change), so we don't depend on exit events. Costs more Firestore writes per solve.
  2. Save progress on visibilitychange events when dontPauseOnLostFocus is set. Right now that save sits inside autoPause, so non-autopause users get no save when the page is hidden.

An alternative approach would be to make the exit requests more reliable:

Making the write survive the unload

  • fetch with keepalive: true is the main option: the browser finishes the request after the page is gone. The Firestore SDK can't do this, so it means sending the play another way, either to Firestore's REST API or to a small API route that does the write. Things to handle:
    • the auth token has to be on hand already, because getIdToken() is async and can't be awaited during unload;
    • the body is capped at about 64 KB, which a play fits comfortably.
  • navigator.sendBeacon is similar but can't set an Authorization header, so the token would have to go in the body to your own endpoint.

The db write on page exit isn't reliable, so on reload the db play could
be behind (or missing) while local storage had the latest progress. Use
whichever is fresher and write the cached play back to the db on load.

Co-Authored-By: Claude <noreply@anthropic.com>
@mdirolf

mdirolf commented Nov 13, 2026

Copy link
Copy Markdown
Member

Thank you!

@mdirolf
mdirolf merged commit ebafffe into crosshare-org:master Nov 13, 2026
1 check passed
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