Test: Safe Redirect Pattern

Current Page: redirect-safe

✅ RECOMMENDED - Safe Redirect Pattern

This pattern safely redirects on back navigation without browser issues.

  • Handler returns false immediately (no async)
  • Modal opens via overlay.open() (not openAsync)
  • router.push() is called from button click (outside handler)
  • Works reliably after page refresh

How it works:

useRegisterBackNavigationHandler(() => {
  // 1. Open modal (fire-and-forget, no await)
  overlay.open(({ isOpen, close }) => (
    <RedirectModal
      isOpen={isOpen}
      close={close}
      onConfirm={() => router.push("/nohandler")}  // 3. User clicks → navigate
    />
  ));
  
  // 2. Return immediately - handler is DONE
  return false;
});

Why this is safe:

  1. Handler returns false synchronously → back navigation blocked
  2. Modal is displayed to user
  3. User clicks button → router.push() is a new user-initiated navigation
  4. This is NOT inside the handler context anymore
  5. Browser treats it as a legitimate navigation
Back to Home