Skip to content

feat(web): ask before leaving a page with unsaved changes - #674

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:feat/unsaved-changes
Oct 10, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:feat/unsaved-changes

Conversation

@LeMyst

@LeMyst LeMyst commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

Closes #519.

Sometimes I click too fast and my latest changes aren't saved. Would it be possible to immediately save when pressing the Back button before leaving the page please?

The editor autosaves a second after the last keystroke. Its Back link left at once, so what was typed in that second was lost. Following the thread (a warning before losing input), this also asks before leaving any page that holds something not saved yet.

What changes

Editor Back saves what is waiting for the autosave, then leaves. If the save fails, it stays on the page with the usual "Save failed" toast, so nothing is lost. A Ctrl/middle click (new tab) is left alone.

Leaving any page with something unsaved asks first: closing the tab or window, the browser's Back, a reload, a link, a keyboard shortcut. The browser shows its own "Leave site?" dialog; no browser lets a page choose the text, so there are no new translations.

static/js/unsaved.js (loaded by base.html, not in static builds) decides what counts:

Counts Where it is used
a write sent with fetch (not GET/HEAD) until it answers every page — shopping list ticks, planner moves, pantry/aisle changes, picture uploads — with no page code
an element with data-dirty pantry.conf and aisle.conf Text tabs (they already set it); the aisle ingredient editor while its names differ from the saved ones
a data-unsaved form, editor or field, on screen and changed since first focused or clicked pantry add forms, item editor and section rename; aisle add forms, ingredient editor, rename and "add as names"; the New recipe / menu / plan form; the editor's rename dialog
CookUnsaved.watch(fn) the recipe editor's text against what was last saved

Not asking when the page moves on by itself: Back after saving, delete and rename go through CookUnsaved.leave(url); a form posted the ordinary way (New, sign-in) is let through. A form a script submits counts as handed over: the page shows its own error if that fails. Search boxes, scale steppers, filters and the recipe picker are not marked.

Ctrl+S, the picture dialog and rename now share saveNow(), which waits for a save already on its way. Before, Ctrl+S during a save did nothing and the newer text waited for the next keystroke.

CLAUDE.md documents the markers next to the other site-wide rules, so new forms opt in.

Tests

tests/e2e/unsaved-changes.spec.ts (12, Chromium), using page.close({ runBeforeUnload: true }):

  • Back saves text typed inside the autosave second; Back stays when the save fails (500)
  • closing asks while a save cannot finish, and does not once it is saved; rename moves on without asking
  • pantry: no question when nothing was typed; asks with a new item typed, while an add is on its way (and not after), and with an unsaved Text tab edit
  • aisles: asks after removing a name in the ingredient editor, not once it is put back
  • New: asks with a name typed; its own submit goes through without a question

The pantry and New tests answer the writes themselves, so seed/ is not changed.

Locally:

  • cargo fmt --check, cargo clippy --all-targets and cargo test are clean.
  • The full Chromium project: 291 passed, 5 skipped, 10 failed.
  • Re-run on their own, all 10 pass:
    • the shopping-list and scaling specs, which share .shopping-list when run in parallel;
    • the slow picture-scaling test;
    • static-file-search, which needs the worktree mounted at its real path in the Playwright container.

🤖 Generated with Claude Code

The editor's Back link left at once, so whatever was typed in the second
before the autosave was lost (cooklang#519). Back now saves first, and stays on the
page if that save fails.

Closing the tab, the browser's Back or any other link asks first while
something is not saved yet, on every page that takes input:

- static/js/unsaved.js, loaded by base.html outside static builds, counts a
  write sent with fetch until it answers, an element with `data-dirty`, a
  `data-unsaved` form or editor changed since it was first touched, and
  checks a page adds with `CookUnsaved.watch()`;
- the recipe editor watches its text; the pantry and aisle pages mark their
  add forms, inline editors and renames (their Text tabs already set
  `data-dirty`); the new file form and the rename dialog are marked too;
- pages that move on by themselves after saving (Back, delete, rename) go
  through `CookUnsaved.leave()`, and a form posted the ordinary way is let
  through.

Ctrl+S, the picture dialog and rename share `saveNow()`, which waits for a
save already on its way instead of dropping the newer text.

Closes cooklang#519

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review October 10, 2026 20:09
@LeMyst
LeMyst merged commit 845a8f2 into cooklang:main Oct 10, 2026
6 checks passed
@LeMyst
LeMyst deleted the feat/unsaved-changes branch October 10, 2026 20:33
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.

Feature request: Save on closing web editor with back button

1 participant