Downloads go through fetch + Blob so they are observable and fail loudly¶
Problem¶
An anchor with a download attribute is a download navigation: the browser
hands it to its download manager outside the page's fetch pipeline. DevTools
Network does not list it, the page never sees the response, and a 401 or 404
becomes a failed entry in the download shelf that nobody reports. Six call
sites did this by hand (useUploadCard, useModuleConfig,
useSubmoduleConfig, PipelineOperationsConsolePage,
ReductionObjectivesSection, the template download in ModuleTable), four of
them carrying a ?d=true query so the backend sends Content-Disposition
because Safari ignores a.download on a plain navigation; the others had that
Safari bug live. Four more sites already fetched a Blob and inlined the same
six anchor lines.
Decisions¶
- One
downloadBlob(blob, filename)inutils/download.ts(renamed fromcsvDownload.ts: it now serves JSON and PDF exports too). Object URLs honora.downloadin every browser, so no server header is needed. The object URL is revoked 40 s later, not synchronously: Firefox can abort a large download when the URL dies before its download manager reads the blob (FileSaver does the same). downloadFrom(load, filename)wraps the load: on failure it toastscommon_download_failedand rethrows, so the user sees something and Sentry gets the error. The one place download errors become visible.api/files.tswithfetchFile(path)anddownloadFile(path)through the ky client: auth and refresh hooks apply, a non-2xx throws, and the file keeps its base name.composables/downloadLastCsv.tsis the single upload-card download, replacing three identical copies.- Templates fetch too.
constant/templateAssets.tsexposesfetchTemplate(fileName)with aresponse.okcheck. Rawfetchon purpose: a same-origin static asset with no auth, and the ky client'sprefixUrlwould rewrite the hashed/assets/path. ?d=trueis gone from the frontend. The backend flag and its regression tests stay: nothing in the app calls it, but it is a public URL feature and removing it is a separate decision.- Handlers become
async; a rejected download surfaces as an unhandled rejection, which the Sentry boot captures, after the toast. Nono-floating-promisesrule is configured, so no call-site changes were needed.
Verification¶
make lint and make type-check pass. The integration test for the
"download last CSV" button now asserts the API request (path, no ?d=true)
and the suggested file name instead of the anchor URL, which a blob: URL
could not satisfy.
Not done¶
chartDownload.tskeeps its own anchor: it downloads adata:URL from the chart canvas, no network involved.- Removing the backend
?d=trueflag. Decide separately.