fix(desktop): preview refresh asks before leaving a page that blocks unload #44433
Openipanasenko(GitHub profile, opens in a new tab)wants to merge 14 commits into
main fromrefs/pull/44433/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/preview/Manager.ts line 2334OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2190,16 +2319,16 @@21902319 ] as const;21912320 });21922321 yield* emitIfCurrent(tabId, pending);2193- if (pending.webContentsId == null) return;2322+ if (pending.webContentsId == null) return true;21942323 const webContentsId = pending.webContentsId;21952324 const wc = webContents.fromId(webContentsId);21962325 if (!wc || wc.isDestroyed()) {21972326 yield* detachDestroyedWebContents(tabId, webContentsId);2198- return;2327+ return true;21992328 }22002329 yield* leaveBusyPage(wc);22012330 if (wc.getURL() === url) {22022331 yield* attempt({ operation: "navigate.reload", tabId, webContentsId: wc.id }, () =>2203- wc.reload(),2332+ navigateAway(wc, () => wc.reload()),22042333 );2205- return;2334+ return true;
juliusmarminge added labelstakeover:in-progress
- @@ -1386,0 +1493,21 @@1493+ let activeIndex = 1;1494+ const goBack = vi.fn(() => void blockUnload(preview));1495+ Object.assign(preview.webContents, {1496+ navigationHistory: {1497+ canGoBack: () => true,1498+ canGoForward: () => false,1499+ getActiveIndex: () => activeIndex,1500+ goBack,1501+ },1502+ });1503+ let answered = false;1504+ showMessageBox.mockImplementationOnce(async () => {1505+ activeIndex = 2;1506+ answered = true;1507+ changes.notify();1508+ return { response: 0, checkboxChecked: false };1509+ });1510+1511+ yield* manager.goBack("tab_guarded_history");1512+ yield* changes.until(() => answered);1513+ yield* flushMicrotasks();
tanstack-source added labelsbug
Pushed604162c
- @@ -525,4 +525,7 @@525525 };526526 const moreMenuActions: PreviewMoreMenuActions | null = serverOwnsRendering527527 ? {528- hardReload: () => void adjustServerTab({ hardReload: true }),528+ // A native tab reloads through the desktop so a page's leave prompt can answer it.529+ hardReload:530+ nativeServerTab && previewBridge531+ ? desktopCall(previewBridge.hardReload)
Pushed9443624
- @@ -605,0 +635,21 @@635+ blocked: false,636+ ...(decision ? { decision } : {}),637+ };638+ leaveAttempts.set(wc.id, leave);639+ return leave;640+ };641+ const navigateAway = (wc: Electron.WebContents, action: () => void) => {642+ armLeave(wc, () => {643+ action();644+ return true;645+ });646+ action();647+ };648+ const traverseHistory = (wc: Electron.WebContents, direction: "back" | "forward") => {649+ const history = wc.navigationHistory;650+ if (direction === "back" ? !history.canGoBack() : !history.canGoForward()) return;651+ const go = () => (direction === "back" ? history.goBack() : history.goForward());652+ const from = history.getActiveIndex();653+ // The page may move its history while the prompt is open; Leave must not land elsewhere.654+ armLeave(wc, () => {655+ if (history.getActiveIndex() !== from) return false;@@ -1281,0 +1350,21 @@1350+ type: "question",1351+ signal,1352+ buttons: [1353+ m(msg("Leave", { $context: "button, leaves a web page" })),1354+ m(msg("Stay", { $context: "button, stays on a web page" })),1355+ ],1356+ defaultId: 1,1357+ cancelId: 1,1358+ message: m(msg("Leave this page?")),1359+ detail: m(msg("Changes you made may not be saved.")),1360+ },1361+ // An unowned box runs synchronously on macOS; an owned one froze mouse input on Windows.1362+ hostPlatform === "win32" ? undefined : BrowserSession.previewHostWindow(wc),1363+ );1364+ return response === 0;1365+ }).pipe(Effect.scoped);1366+ const confirmLeave = Effect.fn("PreviewManager.confirmLeave")(function* (1367+ retry: () => boolean,1368+ askedDocumentId: number,1369+ ) {1370+ const leave = yield* Effect.raceFirst(askToLeave, tabDestroyed.pipe(Effect.as(false))).pipe(
juliusmarminge added labelstakeover:needs-julius
juliusmarminge removed labelstakeover:in-progress
Pushed2eb23f2
Pushed9ee8e67
- @@ -2190,12 +2324,12 @@21902324 ] as const;21912325 });21922326 yield* emitIfCurrent(tabId, pending);2193- if (pending.webContentsId == null) return;2327+ if (pending.webContentsId == null) return true;21942328 const webContentsId = pending.webContentsId;21952329 const wc = webContents.fromId(webContentsId);21962330 if (!wc || wc.isDestroyed()) {21972331 yield* detachDestroyedWebContents(tabId, webContentsId);2198- return;2332+ return true;21992333 }22002334 yield* leaveBusyPage(wc);22012335 if (wc.getURL() === url) {
Sign in to join this conversation.




Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review.
Walkthrough
Preview navigation now prompts when a page blocks unload. Leave retries eligible navigation, reload, or history actions; Stay cancels the action. The navigation result returns through IPC, and the web preview stops follow-up actions when navigation returns false.
Changes: Guarded preview navigation
apps/desktop/src/preview/Manager.ts,apps/desktop/src/preview/GuestInput.ts,apps/desktop/src/preview/GuestInput.test.ts,apps/desktop/src/preview/Manager.test.ts,apps/desktop/src/preview/BrowserSession.tsPreviewManagertracks leave attempts and prompts when a page blocks unload. Leave retries eligible navigation, reload, or history actions; Stay cancels the action. Guarded page reloads use the same handling. Tests cover these outcomes, stale attempts, and load failures.previewHostWindowis now exported.packages/contracts/src/ipc.ts,apps/desktop/src/ipc/methods/preview.ts,apps/web/src/components/preview/PreviewView.tsx,apps/web/src/components/preview/PreviewView.browser.test.tsx,apps/desktop/src/i18n/translations/*.jsonEstimated code review effort: 4 (Complex) | ~45 minutes
Change: Bug fix
Sequence Diagram(s)#
Suggested reviewers:
juliusmarmingeMerge Risk:
Low · previous review
9ee8ePreview refresh and navigation now ask before leaving a page that blocks unload. A few narrow edge cases remain: a stale prompt on redirect, a history retry after the destination changes, and a test that may not catch regressions reliably. These are low risk and can be followed up.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: the desktop preview refresh now shows a Leave/Stay prompt when a page's beforeunload guard blocks unload. The raw summary confirms this change, which adds the async prompt, retry handling, and a navigate() return value. The title is conventional-commit formatted and specific. It is 75 characters, which is acceptable.
Full details: Description check
Explanation
The description follows the repository template closely. It has Problem, Change, Scope, and Verification sections, and it names the model and harness used. The Problem section gives the cause, a repro, and expected behavior. The Change section explains the async Leave/Stay flow and why it is async. The Scope section explains why no issue is linked, which the template allows for small obvious fixes. The Verification section gives before/after results, a screenshot, Electron probe results, test counts, and what was not checked. The description has a few minor inconsistencies with the PR objectives: it reports Manager.test.ts at 100/100, while the objectives say 99/99. The description also has a non-template 'Merge Danger' section, which is extra but not harmful. These gaps are minor and do not block merge review.
Finishing Touches
Generate unit tests (beta)
Fix failing CI checks
a8c259ac-16cd-450e-bc88-bbc43890bb8aReviewing files that changed from the base of the PR and between 2eb23f2538b7ea1a50a9f801f1ea23bbc0cd6d91 and 9ee8e67ccff54f8bb41aa87d7133e46a9f9209a0.
apps/desktop/src/preview/BrowserSession.tsapps/web/src/components/preview/PreviewView.tsxpackages/contracts/src/ipc.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.