Skip to content

Keep unsaved Source edits across an iOS tab swipe — beforeunload, their only guard, never fires there #167

Open

Description

@bencode

Problem

Unsaved Source edits are guarded by exactly one mechanism: a beforeunload listener that asks the browser to confirm leaving (ui-packages/web/src/features/workspace/workspace-context.tsx:96-104). The drafts themselves are in-memory state — sourceDrafts in the workspace store (ui-packages/web/src/features/workspace/workspace-store.ts:70-75, updated only in state, lines 104-163) — and the persisted workspace shape has no place for them (ui-packages/web/src/features/workspace/workspace-storage.ts:6-13). On desktop this works: closing the tab shows the leave-site dialog. On iPhone/iPad it does not: iOS Safari does not show beforeunload dialogs and fires nothing when a tab is swiped away or the app is reclaimed — a documented WebKit limitation, unchanged for the standalone PWA. A reader who edits a note's Source on iOS and switches apps or swipes the tab loses every keystroke with no warning, on a platform the product explicitly targets (narrow layout with overlay panels, workbench.tsx:32-38; coarse-pointer hit targets, styles.css:778-782). The tutorial even states the limitation as fact — "Unsaved Source drafts and Lab state live only in the open page" (tutorial/guide/Your data and models.md:9) — while the app's own desktop guard shows the intent is to protect unsaved work.

Proposal

  • Persist dirty source drafts (fileId → content, base revision, version) when the page is being hidden or unloaded via pagehide / visibilitychange — the events iOS Safari does fire — to browser storage (IndexedDB or localStorage), not to any file system.
  • On reopen, restore the draft as exactly what it is: an unsaved Source draft, with the existing affordances (dirty dot on the tab, Save button, incoming-changes flow untouched). No dialog, no auto-save to the library.
  • Keep the desktop beforeunload guard as it is.

Goal

After this, a reader who edits a document's Source on iOS and loses the tab to a swipe or app reclaim finds the edits back when the document reopens, still marked unsaved; desktop behavior is unchanged, and nothing is written to the library without Save.

Verification

grep -rqiE "draft" ui-packages/web/src/features/workspace/workspace-storage.ts
pnpm --filter @gamma-reader/web test --silent
pnpm check

Refs

  • ui-packages/web/src/features/workspace/workspace-context.tsx:96-104 — beforeunload is the only guard for dirty drafts
  • ui-packages/web/src/features/workspace/workspace-store.ts:70-75,104-163 — drafts live in memory only
  • ui-packages/web/src/features/workspace/workspace-storage.ts:6-13 — persisted workspace has tabs/positions, no drafts
  • tutorial/guide/Your data and models.md:9 — the limitation stated, not addressed
  • ui-packages/web/src/shell/workbench.tsx:32-38 and styles.css:778-782 — narrow mode and coarse-pointer support mark mobile as a target
  • Remember where the reader stopped — reopen documents at their page or scroll position #40 — the pattern of persisting reader state across sessions (reading positions) already exists
  • Show storage durability and pressure before the browser can evict the reader's library #38 — adjacent face: that issue displays storage risk; this one prevents a concrete, platform-specific loss
  • WebKit: beforeunload dialogs are not implemented on iOS; tab dismissal from the app switcher fires no event

Activity

  1. added
    enhancement
    source:scheduled
    on Oct 22, 2026
  2. bencode commented on Oct 22, 2026

    @bencode
    OwnerAuthor

    Independent review — posted by the scheduled issue-reviewer loop, independent of the authoring session. Audited against main@ebb0ee0.

    Evidence audit: all citations confirmed.

    • ✅ workspace-context.tsx:96-104 — beforeunload + preventDefault() gated on hasDirtySource is the only guard.
    • ✅ workspace-store.ts:70-75 — sourceDrafts is in-memory store state; every draft change is a set(), and the store makes no persistence call at all.
    • ✅ workspace-storage.ts:6-13 — persisted shape is exactly { tabs, lastActiveId, positions }; no draft field.
    • ✅ Tutorial quote confirmed at tutorial/guide/Your data and models.md:9 ("Unsaved Source drafts and Lab state live only in the open page" — the bold markers around "Source" hide it from plain-text grep).
    • ✅ Mobile-target evidence: workbench.tsx:30-38 narrow mode via matchMedia; styles.css:778-782 @media (pointer: coarse) 32px hit targets.

    A nuance the implementation should absorb: use-workspace.ts:109-131 already listens for pagehide — its flush writes only { tabs, lastActiveId, positions } (:115). The delivery vehicle half-exists; the work is carrying sourceDrafts into that persistence path and restoring them. On iOS, treat visibilitychange → hidden as the primary trigger (a swipe-away backgrounds the page reliably); pagehide is best-effort. On restore, decide whether the draft's incoming state persists or is re-derived from base.revision — the incoming-changes flow must not silently resolve.

    Verification block repaired (logged here per policy): the original first line — grep -rq "pagehide\|visibilitychange" ui-packages/web/src/features/workspace — passed on unmodified main (use-workspace.ts:125 already registers pagehide), so verify reported "implemented" with nothing implemented: the same non-discriminating-anchor disease as #157/#158, in false-pass form. Replaced with:

    grep -rqiE "draft" ui-packages/web/src/features/workspace/workspace-storage.ts

    The persisted-workspace module is where draft survival must be encoded and contains no "draft" today. Re-linted ✓; verify now correctly fails on main ✓.

    Verdict: valid, keep open. The premise matches documented WebKit behavior (no beforeunload dialog on iOS; tab dismissal delivers nothing dependable). Sharpening: the load-bearing half is the restore path — a test that reopens with a persisted dirty draft and finds it again (still dirty, still unsaved, library untouched) pins the Goal the way #158's store-level test does.

    Duplicate check: none. #40 (closed, reading positions) is the persistence pattern, correctly cited; #38 (closed, storage risk display) is the adjacent face, correctly cited; nothing open or closed covers draft survival.

  3. bencode commented on Oct 26, 2026

    @bencode
    OwnerAuthor

    Independent re-review — posted by the scheduled issue-reviewer loop, independent of the authoring session. Audited against main@23a11fc (2026-10-26), four days of commits after the last review.

    Evidence re-audit: all anchors still hold.

    • workspace-context.tsx:96–104 — beforeunload + preventDefault() gated on hasDirtySource remains the only guard.
    • workspace-storage.ts:6–10 — persisted shape is still exactly { tabs, lastActiveId, positions }; no draft field.
    • use-workspace.ts:109–131 — the pagehide flush (:125) still writes only { tabs, lastActiveId, positions } (:115); drafts are not carried.
    • Tutorial limitation statement confirmed verbatim at tutorial/guide/Your data and models.md:9.

    Sensor: lint ✓; verify ✗ on main for the right reason — workspace-storage.ts contains no "draft" today, so the anchor stays deliverable-shaped.

    What the new commits change: none implement this, but a38c02d (PR #217, 2026-10-26) landed the same flush-on-hide delivery pattern for the conversation exchange — the question is written as sent and the streamed reply flushed as the tab hides or goes (use-conversation/run.ts, navigation.ts). Two in-repo models now exist for the trigger this issue needs; the implementation can mirror a38c02d's hide + pagehide choice so drafts and exchanges survive the same events. Family map stays clean: #197 (closed, implemented by a38c02d) is the conversation face; #212 is update-discards-edits (a confirmation, not persistence); neither covers Source drafts.

    Sharpening (added to the prior review's restore-path test): Your data and models.md:9 states the limitation as fact — "Unsaved Source drafts and Lab state live only in the open page". When this lands that sentence becomes half-false, so the change's scope must include splitting it (drafts persist; Lab state stays ephemeral), or the docs will contradict the behavior.

    Verdict: valid — stays open.

  4. bencode commented on Oct 30, 2026

    @bencode
    OwnerAuthor

    Independent re-review — posted by the scheduled issue-reviewer loop, independent of the authoring session. Audited against main@d05b6b6 (2026-10-28), two days of commits after the last review.

    Evidence re-audit: all anchors hold in substance; two line drifts, none behavioral.

    • workspace-context.tsx:102-103 — the beforeunload listener is still the only guard, still gated on hasDirtySource (cited :96-104; block shifted).
    • workspace-store.ts:79 — sourceDrafts: {} in-memory only; workspace-storage.ts:6-10 — SavedWorkspace = { tabs, lastActiveId, positions }, still no draft field.
    • use-workspace.ts:125,128 — the pagehide flush still writes only { tabs, lastActiveId, positions }.
    • tutorial/guide/Your data and models.md:9 — "Unsaved Source drafts and Lab state live only in the open page" verbatim, unchanged.
    • Drift to log: the mobile-target anchor styles.css:778-782 — @media (pointer: coarse) now sits at styles.css:920 (moved by 93563a2, 2026-10-23). Substance intact.

    Commits since the last review: only b263043 (PR #234, open a cited PDF at its page) touched features/workspace/ at all — use-workspace-tools.ts and reader-context.test.tsx, orthogonal to draft persistence. None of the issue's anchors moved; the plugins merge (de9243b, PR #279) does not touch this path.

    Sensor: lint ✓; verify ✗ on main for the right reason — workspace-storage.ts still contains no "draft", so the anchor stays deliverable-shaped.

    Duplicate re-scan (open + closed): clean. #197 (closed, exchange persistence, implemented by a38c02d) remains the conversation face; nothing covers Source drafts.

    Verdict: valid — stays open. Prior sharpening stands unchanged: pin the restore path with a test (reopen with a persisted dirty draft → still dirty, library untouched), and split the tutorial sentence at Your data and models.md:9 when this lands.

  5. bencode commented on Nov 3, 2026

    @bencode
    OwnerAuthor

    Posted by the scheduled issue-reviewer loop, independent of the authoring session.

    Re-audit @ d05b6b6 — fourth pass, quiet window (0 commits since the last re-audit at the same HEAD; no issue filed in any state since 2026-11-01). Still valid; stays open; no edits.

    • Anchors independently re-opened, verbatim. workspace-context.tsx:100-105 — beforeunload + preventDefault() gated on hasDirtySource remains the only guard; workspace-store.ts:31,79 — sourceDrafts in-memory only; workspace-storage.ts:6-13 — SavedWorkspace = { tabs, lastActiveId, positions }, no draft field; use-workspace.ts:111-131 — the pagehide flush still writes only those three; tutorial/guide/Your data and models.md:9 — "Unsaved Source drafts and Lab state live only in the open page" unchanged.
    • Sensor. lint ✓; verify ✗ on main ✓ — workspace-storage.ts still contains no "draft"; the repaired anchor stays deliverable-shaped.
    • Duplicates. Clean, refreshed — Persist the exchange while it happens — a crash or phone tab-kill mid-answer loses the question and the partial reply, leaving a titled conversation that opens empty #197 (closed, exchange persistence via a38c02d) remains the conversation face; nothing new filed since 2026-11-01 covers Source drafts.
    • Sharpenings stand. Pin the restore path with a test (reopen with a persisted dirty draft → still dirty, still unsaved, library untouched), and split the tutorial sentence at Your data and models.md:9 when this lands so docs don't contradict the behavior.
  6. bencode commented on Nov 14, 2026

    @bencode
    OwnerAuthor

    Posted by the scheduled issue-reviewer loop, independent of the authoring session.

    Re-audit @ d05b6b6 — fifth pass, quiet window (0 commits since the fourth pass at the same HEAD on 2026-11-03; nothing filed in any state since #295, 2026-11-05, and it touches another surface). Still valid; stays open; no edits.

    • Anchors independently re-opened, verbatim. features/workspace/workspace-context.tsx:99-105 — beforeunload + preventDefault() gated on hasDirtySource remains the only guard; workspace-store.ts:31,79 — sourceDrafts still in-memory only; workspace-storage.ts:5-9 — SavedWorkspace = { tabs, lastActiveId, positions }, no draft field (grep draft in that file: 0 matches); use-workspace.ts:109-131 — the pagehide flush still writes only those three; tutorial/guide/Your data and models.md:9 — "Unsaved Source drafts and Lab state live only in the open page" unchanged; the logged drift styles.css:920 (@media (pointer: coarse)) still where the last review placed it.
    • Sensor. lint ✓; verify ✗ on main ✓ — the repaired anchor (grep -rqiE "draft" workspace-storage.ts) stays deliverable-shaped.
    • Duplicates. Clean, refreshed — Persist the exchange while it happens — a crash or phone tab-kill mid-answer loses the question and the partial reply, leaving a titled conversation that opens empty #197 (closed, exchange persistence via a38c02d) remains the conversation face; nothing new covers Source drafts.
    • Sharpenings stand. Pin the restore path with a test (reopen with a persisted dirty draft → still dirty, still unsaved, library untouched), and split the tutorial sentence at Your data and models.md:9 when this lands.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementsource:scheduled

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions