fix(editor): PDF export download keeps its URL alive (WebKit may cancel)

This commit is contained in:
bchanot committed 2026-10-18 04:53:45 +02:00
1 parent ae17097afb
commit 2b8000e5ab
3 files changed
+32 -15

No files matched your search

+5 -7
View File
@@ -33,6 +33,7 @@ import type { SheetIndex } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import type { ViewStates } from '@/anatomy/drawing'
import { exportReportToPdf } from '@/pdf/export'
import { downloadBlob } from '@/lib/download'
import type { ExportWarning } from '@faunosteo/pdf'
import type { RebaseOutcome } from '@faunosteo/core-wasm/types'
import { useT } from '@/i18n/useT'
@@ -236,13 +237,10 @@ export default function EditorRoute() {
const { pdf, warnings } = await exportReportToPdf(report, index, {
locale,
})
const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `bilan-${report.animal.name}-${report.date}.pdf`
a.click()
URL.revokeObjectURL(url)
downloadBlob(
new Blob([pdf as BlobPart], { type: 'application/pdf' }),
`bilan-${report.animal.name}-${report.date}.pdf`,
)
const missing = missingItemsOf(warnings)
if (missing.length) {
window.alert(
+17 -7
View File
@@ -3,18 +3,28 @@ import { todayIso } from '@/lib/dates'
const REVOKE_DELAY_MS = 30_000
/**
* Saves `raw` as `<stem>-<date>.json`. A lone surrogate in a JSON.stringify output is a
* `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a
* deletion: the anchor is attached while clicked and the URL revoked later, since some
* engines (WebKit) cancel a download whose URL is revoked at once.
* Saves `blob` under `fileName`. The anchor is attached while clicked and the URL revoked
* later, since some engines (WebKit) cancel a download whose URL is revoked at once.
*/
export function downloadJson(raw: string, stem: string): void {
const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' }))
export function downloadBlob(blob: Blob, fileName: string): void {
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `${stem}-${todayIso()}.json`
a.download = fileName
document.body.appendChild(a)
a.click()
a.remove()
setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS)
}
/**
* Saves `raw` as `<stem>-<date>.json`. A lone surrogate in a JSON.stringify output is a
* `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a
* deletion.
*/
export function downloadJson(raw: string, stem: string): void {
downloadBlob(
new Blob([raw], { type: 'application/json' }),
`${stem}-${todayIso()}.json`,
)
}
@@ -31,6 +31,7 @@ const PDF_BYTES = new Uint8Array([37, 80, 68, 70])
let sheet: SheetIndex
const downloads: string[] = []
const clicked: { connectedAtClick: boolean }[] = []
const realCreateObjectURL = URL.createObjectURL
const realRevokeObjectURL = URL.revokeObjectURL
@@ -48,17 +49,20 @@ beforeEach(() => {
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
exportMock.mockReset()
downloads.length = 0
clicked.length = 0
URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test')
URL.revokeObjectURL = vi.fn()
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
this: HTMLAnchorElement,
) {
downloads.push(this.download)
clicked.push({ connectedAtClick: this.isConnected })
})
vi.stubGlobal('alert', vi.fn())
})
afterEach(() => {
cleanup()
vi.useRealTimers()
vi.restoreAllMocks()
vi.unstubAllGlobals()
URL.createObjectURL = realCreateObjectURL
@@ -94,7 +98,8 @@ describe('EditorRoute export', () => {
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
})
it('downloads bilan-<animal>-<date>.pdf through a blob URL it then revokes', async () => {
it('downloads bilan-<animal>-<date>.pdf through a blob URL it revokes after a delay', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
await mountAndExport(fr['editor.export']!)
await waitFor(() => expect(downloads).toHaveLength(1))
@@ -102,6 +107,10 @@ describe('EditorRoute export', () => {
expect(URL.createObjectURL).toHaveBeenCalledTimes(1)
const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob
expect(blob.type).toBe('application/pdf')
expect(clicked[0]!.connectedAtClick).toBe(true)
expect(document.body.querySelector('a[download]')).toBeNull()
expect(URL.revokeObjectURL).not.toHaveBeenCalled()
vi.advanceTimersByTime(30_000)
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test')
expect(window.alert).not.toHaveBeenCalled()
})