Repository navigation
feat(web): ask before leaving a page with unsaved changes - #674
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #519.
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 bybase.html, not in static builds) decides what counts:fetch(not GET/HEAD) until it answersdata-dirtydata-unsavedform, editor or field, on screen and changed since first focused or clickedCookUnsaved.watch(fn)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), usingpage.close({ runBeforeUnload: true }):The pantry and New tests answer the writes themselves, so
seed/is not changed.Locally:
cargo fmt --check,cargo clippy --all-targetsandcargo testare clean..shopping-listwhen run in parallel;static-file-search, which needs the worktree mounted at its real path in the Playwright container.🤖 Generated with Claude Code