From 35f246b7f29ef0e509662be1f68844774f604cad Mon Sep 17 00:00:00 2001 From: Arda Erzin Date: Thu, 11 Dec 2025 17:27:46 +0300 Subject: [PATCH] Add unhandledrejection event handler to suppress route cancellation errors in useBlockNavigation - Add event listener for unhandledrejection to prevent console errors when intentionally canceling route changes - Emit routeChangeError event before throwing cancelRouteChange to properly signal Next.js router - Clean up unhandledrejection listener in useEffect cleanup function - Update comment formatting for consistency --- web/oss/src/hooks/useBlockNavigation.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/web/oss/src/hooks/useBlockNavigation.ts b/web/oss/src/hooks/useBlockNavigation.ts index 7bfcdf2a49..9785523ab8 100644 --- a/web/oss/src/hooks/useBlockNavigation.ts +++ b/web/oss/src/hooks/useBlockNavigation.ts @@ -43,6 +43,15 @@ const useBlockNavigation = ( }, [_shouldAlert]) useEffect(() => { + // Handle unhandled promise rejections for our intentional route cancellation + const handleUnhandledRejection = (event: PromiseRejectionEvent) => { + if (event.reason === "cancelRouteChange") { + event.preventDefault() + } + } + + window.addEventListener("unhandledrejection", handleUnhandledRejection) + const handler = (newRoute: string) => { if (opened.current || !blocking.current) return @@ -84,13 +93,15 @@ const useBlockNavigation = ( cancellable: false, }) - //block NextJS navigation until user confirms or cancels + // Block NextJS navigation until user confirms or cancels + Router.events.emit("routeChangeError") throw "cancelRouteChange" } Router.events.on("routeChangeStart", handler) return () => { window.removeEventListener("beforeunload", beforeUnloadHandler) + window.removeEventListener("unhandledrejection", handleUnhandledRejection) Router.events.off("routeChangeStart", handler) } }, [])