Skip to content

Commit 23a84c7

Browse files
committed
WEB-109 Cleanup logic, add comment about nextjs internals hack
1 parent 2453627 commit 23a84c7

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/app/editor/[id]/[slug]/useUnsavedChangesGuard.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { useDialogs } from "@/components/ui/dialog-provider";
55

66
const LEAVE_MESSAGE = "You have unsaved changes. Leave this page without saving?";
77

8+
// Next.js internally sets an auto-incrementing `idx` on history.state to track
9+
// navigation position. This is an undocumented internal — not a public API — so
10+
// it may disappear or change in any Next.js release. We use it to determine
11+
// whether the user navigated forward or back so we can revert the navigation
12+
// when they cancel. If `idx` is absent we degrade gracefully (warn, but can't
13+
// revert). See: https://github.com/vercel/next.js/discussions/34980
814
function getCurrentHistoryIndex() {
915
const historyState = window.history.state as { idx?: number } | null;
1016
return typeof historyState?.idx === "number" ? historyState.idx : null;
@@ -20,17 +26,11 @@ export function useUnsavedChangesGuard(isDirty: boolean) {
2026
return true;
2127
}
2228

23-
const shouldLeave = await confirm({
29+
return await confirm({
2430
message: LEAVE_MESSAGE,
2531
actionLabel: "Leave",
2632
destructive: true,
2733
});
28-
29-
if (!shouldLeave) {
30-
return false;
31-
}
32-
33-
return true;
3434
}, [confirm, isDirty]);
3535

3636
useEffect(() => {

0 commit comments

Comments
 (0)