UI + UX Error Prevention And Recovery established

Exit warning

Track departure-risk state precisely, attach browser leave protection only while needed, intercept in-app navigation with a clear decision surface, identify what is at risk, offer save, discard, and stay choices where possible, and clear the warning immediately after work is safe.

Decision first

Choose this pattern when the problem matches

Use when

  • Users have unsaved or pending changes that cannot be recovered if they leave.
  • A route change, tab close, reload, browser Back, external link, or sign-out action can interrupt a task.
  • Autosave, draft state, or server recovery is absent, pending, failed, or insufficient for the current change.
  • The product can identify the dirty scope and provide a clear stay, save, or discard choice.

Avoid when

  • The page is clean, read-only, or all changes are safely saved and recoverable.
  • The product can preserve work automatically and no user-facing loss remains.
  • The warning is being used to keep users from leaving for business metrics rather than data protection.
  • The trigger is an explicit destructive action that should use a confirmation dialog.
  • A durable draft object already exists and users can resume it from a clear draft state.

Problem it prevents

Users can accidentally leave a page, close a tab, reload, follow a link, sign out, or move to another route while work is dirty, pending, failed, or session-bound, causing data or context loss that cannot be recovered.

Pattern anatomy

What a strong implementation has to make clear

User need

The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.

Pattern promise

Track departure-risk state precisely, attach browser leave protection only while needed, intercept in-app navigation with a clear decision surface, identify what is at risk, offer save, discard, and stay choices where possible, and clear the warning immediately after work is safe.

Required state

Clean state with no exit warning attached.

Recovery path

The warning appears after every click even when the page is clean.

Access contract

Use a real dialog or alertdialog for in-app warnings, with a heading that names the loss risk.

Quality bar

The difference between expert and weak execution

Strong implementation

Specific, visible, recoverable

  • A grant form user clicks Back with two unsaved answers; a dialog names the changed section and offers Save and leave, Discard and leave, or Stay editing.
  • A browser reload attempt on a dirty form triggers the browser's leave-page dialog only while the dirty state exists, with the page already showing Last change not saved.
  • A user starts to close a tab after a failed autosave, sees the risk before leaving, stays on the page, retries the save, and then leaves without another warning.
  • A user chooses Discard and leave from an in-app warning and is taken to the intended destination with a clear understanding of what was discarded.
Weak implementation

Vague, hidden, hard to recover from

  • Every navigation shows Are you sure you want to leave? even after the form is saved.
  • A vague warning says Changes may be lost but gives only OK and Cancel, with no save, discard, or stay wording.
  • A user clicks a sidebar link after typing a long answer and loses it because the product only watched final Submit.
  • A user saves a draft but still sees a leave warning on every route change because the dirty flag was never cleared.
UI guidance
  • Show a warning only when the user attempts to leave with real unsaved, pending, failed, or unrecoverable work; place in-app route warnings at the point of departure with Save and leave, Discard and leave, and Stay editing choices.
  • For browser close, reload, or cross-origin navigation, rely on the browser-owned beforeunload dialog and keep an in-page status that already explains what is unsaved because custom dialog text may not be shown.
UX guidance
  • Use exit warning to interrupt an attempted departure that would lose work, context, payment state, upload progress, or session-bound data the product cannot safely recover.
  • Treat exit warning as a last-resort safeguard, not the primary save model: save or draft work when possible, remove the warning as soon as work is safe, and avoid blocking clean navigation.
Implementation contract

What the implementation must handle

States

  • Clean state with no exit warning attached.
  • Dirty unsaved state with local unsaved-work status.
  • Pending autosave or upload state.
  • Failed save state with retry before leaving.

Interaction

  • The warning appears only after a departure attempt or immediately before a known session boundary that could cause loss.
  • The warning identifies the scope of loss, such as Contact details, uploaded evidence, payment setup, or unsaved section answers.
  • Stay editing keeps the user on the current surface with values intact and returns focus to the triggering or first dirty control.
  • Discard and leave clears only the stated unsaved work and then completes the intended navigation.

Accessibility

  • Use a real dialog or alertdialog for in-app warnings, with a heading that names the loss risk.
  • Move focus into the warning when it opens and return focus to the triggering navigation control or dirty field when users stay.
  • Use button labels that include action and consequence, such as Save and leave, Discard and leave, and Stay editing.
  • Do not rely on color, icons, or browser-owned dialogs as the only indication that work is unsaved.

Review

  • What exact work or context would be lost if the user leaves now?
  • Is the loss unrecoverable, or can autosave or draft state make the warning unnecessary?
  • Which exit paths are covered: internal links, Back, reload, close tab, external links, sign out, and session expiry?
  • Does the warning say what will be lost and where the user was trying to go?
Interactive lab

Inspect the states before you copy the pattern

Protect work when leaving

Create unsaved work, attempt an internal route change, browser close, reload, external link, and sign out; choose Stay, Save and leave, or Discard and leave, then compare warn-always, no-warning, vague, trapped, stale-after-save, and toast-only failures.

Exit warning
Interactive demo is ready

Launch the live UI/UX lab when you want to inspect states, keyboard behavior, and common failure modes.

State To Inspect

Clean state with no exit warning attached.

Keyboard / Access

Internal navigation controls activate the guard with Enter or Space when dirty state exists.

Avoid Generating

Warning on every navigation regardless of whether work is dirty.

Evidence trail

Source-backed claims behind this guidance

web.dev: Back/forward cache

web.dev - checked

web.dev documents browser navigation-cache considerations and recommends limiting unload-related handlers.

Full agent/debug reference

Problem Context

  • The task includes typed answers, selected records, uploads, payment setup, edited content, in-progress review, temporary calculations, or session-bound state.
  • The product cannot guarantee recovery through autosave, draft state, server session, local storage, or transaction replay.
  • Users may leave through Back, close tab, reload, external link, sidebar navigation, breadcrumb, sign out, idle timeout, or route change.
  • Browser-owned unload dialogs have limited custom copy and unreliable mobile behavior, so the product still needs local dirty-state messaging and recovery.

Selection Rules

  • Choose exit warning when departure has already been attempted and leaving would lose work or context that is not safely recoverable.
  • Do not show exit warnings for clean state, read-only browsing, already-saved drafts, completed submissions, or changes that autosave can reliably restore.
  • Use autosave form to reduce the need for warnings by saving progress automatically and warning only while saves are pending or failed.
  • Use draft state when the work survives navigation as a durable object with Resume, Discard, Publish, or Compare choices.
  • Use confirmation dialog when the trigger is an explicit action such as Cancel application, Delete file, or Discard draft rather than a navigation attempt.
  • For in-app navigation, show a custom warning that names the affected section, last saved state, and intended destination.
  • For tab close, reload, or browser Back outside app control, attach beforeunload only while dirty state exists and remove it after save or discard.
  • Offer Stay editing as the safest default when data loss is likely; offer Discard and leave only with clear loss scope.
  • Offer Save and leave only when save can complete safely before navigation or when users understand that leaving waits for save completion.
  • If mobile or browser behavior cannot guarantee a warning, preserve local draft state or show earlier in-page risk messaging instead of relying solely on beforeunload.

Required States

  • Clean state with no exit warning attached.
  • Dirty unsaved state with local unsaved-work status.
  • Pending autosave or upload state.
  • Failed save state with retry before leaving.
  • In-app route change attempted state with destination named.
  • Browser close, reload, or external navigation protected state.
  • Save and leave in progress state.
  • Discard and leave confirmation state.
  • Stay editing state that restores focus and preserves values.
  • Saved or submitted state that clears the warning.

Interaction Contract

  • The warning appears only after a departure attempt or immediately before a known session boundary that could cause loss.
  • The warning identifies the scope of loss, such as Contact details, uploaded evidence, payment setup, or unsaved section answers.
  • Stay editing keeps the user on the current surface with values intact and returns focus to the triggering or first dirty control.
  • Discard and leave clears only the stated unsaved work and then completes the intended navigation.
  • Save and leave either completes the save before navigation or explains that saving failed and keeps the user on the page.
  • The browser beforeunload listener is attached only while dirty state exists and removed after save, submit, discard, or reset.
  • A warning never traps users after they explicitly discard or after the work is safely recoverable.

Implementation Checklist

  • Define dirty state from actual data changes, pending uploads, failed saves, in-flight payment setup, or unrecoverable session context rather than from page visit alone.
  • Track the intended destination for in-app navigation so the warning can continue there after save or discard.
  • Keep browser beforeunload protection conditional and remove it as soon as risk clears.
  • Use route guards for internal links, Back controls, breadcrumbs, side navigation, sign out, and cancel routes that could lose current work.
  • Model Save and leave, Discard and leave, Stay editing, Retry save, and Continue to destination as separate actions.
  • Handle pending or failed autosave differently from simple dirty state; do not allow a stale Saved label to suppress the warning.
  • Restore focus after Stay editing and announce the preserved state for assistive technology users.
  • Test browser reload, tab close, mobile app switch, internal route change, external link, sign out, session expiry, failed save, successful save, discard, Back button, keyboard activation, and screen readers.

Common Generated-UI Mistakes

  • Warning on every navigation regardless of whether work is dirty.
  • Using exit warnings for retention, growth, ads, or survey prompts instead of preventing user-facing loss.
  • Leaving beforeunload listeners attached after save, harming navigation and browser-cache behavior.
  • Showing a vague Are you sure? message without naming the affected work or consequence.
  • Providing only Leave and Cancel when Save and leave or Stay editing is technically possible.
  • Trapping users after they choose to discard or after work is saved.
  • Relying only on browser beforeunload even though mobile browsers may not show it reliably.

Critique Questions

  • What exact work or context would be lost if the user leaves now?
  • Is the loss unrecoverable, or can autosave or draft state make the warning unnecessary?
  • Which exit paths are covered: internal links, Back, reload, close tab, external links, sign out, and session expiry?
  • Does the warning say what will be lost and where the user was trying to go?
  • Can users save before leaving, discard intentionally, and stay editing without losing focus or values?
  • Is the browser leave listener removed immediately after save, submit, or discard?
Accessibility
  • Use a real dialog or alertdialog for in-app warnings, with a heading that names the loss risk.
  • Move focus into the warning when it opens and return focus to the triggering navigation control or dirty field when users stay.
  • Use button labels that include action and consequence, such as Save and leave, Discard and leave, and Stay editing.
  • Do not rely on color, icons, or browser-owned dialogs as the only indication that work is unsaved.
  • Announce failed save, pending save, and stay-editing recovery through status text.
  • Ensure keyboard users can trigger and resolve the same warning paths as pointer users.
  • Do not trap keyboard or assistive technology users after they choose to leave.
Keyboard Behavior
  • Internal navigation controls activate the guard with Enter or Space when dirty state exists.
  • Focus moves to the warning heading or first safe action when an in-app warning opens.
  • Escape or Stay editing closes the warning and restores focus without losing values.
  • Tab order places Stay editing, Save and leave, and Discard and leave in a predictable order.
  • Save and leave keeps focus in the warning while saving and either navigates on success or exposes retry on failure.
  • Discard and leave requires a deliberate button activation and then completes the original destination.
Variants
  • Unsaved form exit warning
  • Pending autosave exit warning
  • Failed save leave warning
  • Browser close warning
  • Reload warning
  • In-app route guard
  • External link warning
  • Sign-out with unsaved work warning
  • Upload in progress exit warning
  • Session boundary leave warning

Verification

Last verified: