Files
faunosteo/.claude/tasks/plans/2026-10-18-pdf-download-webkit-0930.md

2.9 KiB

FIX PLAN: PDF export download revoked at once (WebKit may cancel it)

Root cause: apps/app/src/app/routes/EditorRoute.tsx onExport (lines 239-245) builds a detached <a>, clicks it and calls URL.revokeObjectURL(url) on the next line. WebKit (Safari, iOS) can cancel a download whose blob URL is revoked before the download starts reading it; Firefox historically ignored clicks on detached anchors. apps/app/src/lib/download.ts downloadJson (R3, BDR-055) already attaches the anchor while clicking and revokes after REVOKE_DELAY_MS = 30_000.

Edit:

  1. lib/download.ts: new exported downloadBlob(blob: Blob, fileName: string): void = create URL, anchor with href and download, document.body.appendChild(a), a.click(), a.remove(), setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS). downloadJson(raw, stem) becomes downloadBlob(new Blob([raw], { type: 'application/json' }), ${stem}-${todayIso()}.json); its doc comment keeps the lone-surrogate note, the WebKit note moves to downloadBlob.
  2. EditorRoute.tsx: the seven lines become downloadBlob(new Blob([pdf as BlobPart], { type: 'application/pdf' }), bilan-${report.animal.name}-${report.date}.pdf), import from @/lib/download. Warnings alert and error handling unchanged (the alert still follows the click synchronously, as today).
  3. Test routes.EditorRoute.export.test.tsx case "downloads bilan--.pdf through a blob URL it then revokes": fake timers with shouldAdvanceTime: true for that case; the click spy also records this.isConnected; assert connected at click, not revoked right after the download, revoked with blob:faunosteo-test after advancing 30,000 ms.

Challenge summary (correctness SOLID, robustness SOLID, simplicity SOLID; minors folded)

  • Test step 3, closed: keep the downloads array as is; add a separate clicked: HTMLAnchorElement[] filled by the click spy (cleared in beforeEach). In the revoke case: vi.useFakeTimers({ shouldAdvanceTime: true }) BEFORE mountAndExport; after the download, assert clicked[0].isConnected was true at click (record it in the spy as connectedAtClick), document.querySelector('a[download]') is null, URL.revokeObjectURL not called; then vi.advanceTimersByTime(30_000) and assert it was called with blob:faunosteo-test. Add vi.useRealTimers() to the file's afterEach.
  • downloadJson criterion 4 (name <stem>-<date>.json, type, 30 s revoke) is checked by reading the code and by the existing coreStopped / heldSession suites; no scope widening.
  • WebKit: unit tests prove the code shape only. The premise is checked with a Playwright WebKit reproduction (immediate vs delayed revoke) and the fix is validated only by the owner's iPhone run (BDR-057 OD-6): several views, the warnings-alert path, the download prompt left open over 30 s. If that run still fails, the next suspect is the lost user activation between the click and a.click() after await exportReportToPdf.