Skip to content

v1.4.0

Choose a tag to compare

@github-actions github-actions released this 06 Jul 20:07

✨ Feature — @weave-framework/router: async before-leave guards (unsaved-changes prompts)

  • New beforeEach(fn) — an async, cancellable guard that runs before every navigation commits
    (push, replace, and browser back/forward). Route guards are synchronous by design (great for
    auth), so there was no point at which navigation could pause to await a user decision — which is
    exactly what an "you have unsaved changes, really leave?" prompt on a routed page needs. beforeEach
    fills that gap:
    • The guard receives LeaveInfo { to, from, type } and returns boolean | Promise<boolean> — return
      false (or Promise<false>) to cancel; the current path and the address bar stay put.
    • All registered guards must allow for a navigation to proceed; the first false short-circuits.
      beforeEach(fn) returns an unregister function — call it in the page's cleanup so the guard only
      lives while that page is mounted.
    • Browser back/forward is handled too: on a cancelled pop the router rolls history back
      (history.go) so the URL matches staying put — no "content old, address new" half-state.
    • afterEach fires only on a committed navigation (never on a cancelled one); the synchronous
      route guards and matching are unchanged and run only after before-leave allows.
  • New navigate(to, { replace: true }) (and the NavigateOptions type) — swap the current history
    entry instead of pushing, via history.replaceState. This promotes the previously internal-only
    'replace' NavType to real public API.
  • When no beforeEach guard is registered, navigation stays fully synchronous — existing behavior
    and timing are unchanged.