Skip to content

fix(web): unsaved-changes guard covers Back, Forward and the command palette (#439) - #503

Open
mblaauw wants to merge 1 commit into
mainfrom
fix/439-unsaved-changes-navigation
Open

mblaauw wants to merge 1 commit into
mainfrom
fix/439-unsaved-changes-navigation

Conversation

@mblaauw

@mblaauw mblaauw commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Closes #439.

Summary

The guard only handled closing the tab and clicks on links, so pressing browser
Back or jumping to a page with Ctrl/Cmd+K left a dirty form and lost the draft.

  • A capture-phase popstate listener on window runs before Next's router. When
    history moves to another page while the form is dirty it stops the router
    from rendering that page and asks "Leave without saving?". Keep editing puts
    the form's entry back on top of history; Leave page re-dispatches the
    popstate so the router renders the page the browser moved to. Popstates that
    stay on the page (section hashes) pass through untouched.
  • New lib/navigation-guard.ts holds the active guard. The command palette asks
    confirmNavigation() before router.push, so it gets the same dialog. Saving
    still navigates with router.push directly and never prompts.
  • Unit tests for the guard registry and same-page check; manual case added to
    the UI test plan.

Testing

  • pnpm --filter @orbit/web typecheck: passed
  • pnpm --filter @orbit/web test: 99 passed (5 new in lib/navigation-guard.test.ts)
  • pnpm docs:check: passed
  • Manual case added to docs/development/ui-test-plan.md

Not checked in a browser: it relies on the capture-phase listener running before Next's popstate handler and on Next 16 accepting the restored entry. After Keep editing on Back, Forward history is gone, so Forward does nothing rather than prompting.

🤖 Generated with Claude Code

…palette (#439)

The guard only handled closing the tab and clicks on links, so pressing browser
Back or jumping to a page with Ctrl/Cmd+K left a dirty form and lost the draft.

- A capture-phase popstate listener on window runs before Next's router. When
  history moves to another page while the form is dirty it stops the router
  from rendering that page and asks "Leave without saving?". Keep editing puts
  the form's entry back on top of history; Leave page re-dispatches the
  popstate so the router renders the page the browser moved to. Popstates that
  stay on the page (section hashes) pass through untouched.
- New lib/navigation-guard.ts holds the active guard. The command palette asks
  confirmNavigation() before router.push, so it gets the same dialog. Saving
  still navigates with router.push directly and never prompts.
- Unit tests for the guard registry and same-page check; manual case added to
  the UI test plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Unsaved-changes guard misses browser Back and command-palette navigation, so form drafts are lost without a prompt

1 participant