2.9 KiB
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:
lib/download.ts: new exporteddownloadBlob(blob: Blob, fileName: string): void= create URL, anchor withhrefanddownload,document.body.appendChild(a),a.click(),a.remove(),setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS).downloadJson(raw, stem)becomesdownloadBlob(new Blob([raw], { type: 'application/json' }),${stem}-${todayIso()}.json); its doc comment keeps the lone-surrogate note, the WebKit note moves todownloadBlob.EditorRoute.tsx: the seven lines becomedownloadBlob(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).- Test
routes.EditorRoute.export.test.tsxcase "downloads bilan--.pdf through a blob URL it then revokes": fake timers withshouldAdvanceTime: truefor that case; the click spy also recordsthis.isConnected; assert connected at click, not revoked right after the download, revoked withblob:faunosteo-testafter advancing 30,000 ms.
Challenge summary (correctness SOLID, robustness SOLID, simplicity SOLID; minors folded)
- Test step 3, closed: keep the
downloadsarray as is; add a separateclicked: HTMLAnchorElement[]filled by the click spy (cleared in beforeEach). In the revoke case:vi.useFakeTimers({ shouldAdvanceTime: true })BEFOREmountAndExport; after the download, assertclicked[0].isConnectedwas true at click (record it in the spy asconnectedAtClick),document.querySelector('a[download]')is null,URL.revokeObjectURLnot called; thenvi.advanceTimersByTime(30_000)and assert it was called withblob:faunosteo-test. Addvi.useRealTimers()to the file's afterEach. downloadJsoncriterion 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()afterawait exportReportToPdf.