TheRare Visuals

The download button that did nothing

A multi-gigabyte zip download went from crashing the tab, to a hidden iframe that two security headers silently refused, to a plain link, plus a pre-flight request so errors still reach the page.

TheRare Engineering29 October 20264 min readBeginner

The symptom

Click Download all and nothing happens. No error message, no spinner that never ends, nothing on the page at all. The button was for staff: download every raw footage file of a booking as one zip, often several gigabytes.

This is a short post about a fix to a fix, and about two security rules doing exactly their job.

Version one: fetch, then save

The first version did what most tutorials show, roughly:

TypeScript
const response = await fetch(zipUrl);
const blob = await response.blob();   // the whole archive, in memory
saveAs(blob, "raw-footage.zip");

response.blob() waits until the entire response has arrived and holds it in the tab's memory before the save dialog appears. For a 50 MB zip that's fine. For a few gigabytes of wedding footage the tab hangs or crashes with "failed to download zip". (Part 3 covers how little memory a browser tab really has.)

Version two: let the browser download it

Browsers already have a download manager that streams a file straight to disk, shows progress, and can resume. You get it whenever the browser itself loads a URL whose response says "this is an attachment". So the second version stopped downloading in JavaScript and asked the browser to load the URL instead, in a hidden iframe:

TypeScript
const frame = document.createElement("iframe");
frame.style.display = "none";
frame.src = url;
document.body.appendChild(frame);

The hidden iframe is a common trick: the page doesn't navigate away, and the response's Content-Disposition: attachment header turns it into a download. Login still works, because staff are signed in with an httpOnly cookie that the browser sends with this request like any other.

It looked right, and it did nothing.

Two locks on the same door

There were two independent reasons the browser refused, and either one alone was enough.

The site's own rule. The website sends a Content-Security-Policy header, including a frame-src list: the only places this site may load into a frame. It lists our own site plus Razorpay, Google sign-in, Instagram and YouTube embeds. The API runs on a different origin, and it isn't on the list, so the browser refused to load the iframe at all.

The API's rule. The API sends X-Frame-Options: DENY on every response (set in Spring Security's SecurityConfig), which means "never show me inside a frame, on anyone's site". That protects against clickjacking, where a hostile page loads your app invisibly in a frame and tricks you into clicking its buttons. The browser honours it even for a frame our own page created.

Both rules are right. We didn't want to loosen either one to make a download button work.

And because a refused frame doesn't throw an error into the page's JavaScript, the button silently did nothing. That's why the report had no error to quote.

The fix is the oldest tool on the web, a link:

TypeScript
function startNativeDownload(path: string): void {
  const url = path.startsWith("http") ? path : `${API_BASE_URL}${path}`;
  const link = document.createElement("a");
  link.href = url;
  link.rel = "noopener";
  document.body.appendChild(link);
  link.click();
  link.remove();
}

Following a link isn't framing, so neither rule applies. Because the response is sent as an attachment, the browser hands it to the download manager and the page stays exactly where it was. The file streams to disk with a progress bar, at any size.

A link can't tell the page that something went wrong. If the server answers 403 (not allowed) or 404 (no such file), the browser shows its own error in the download manager, and our page never finds out.

So callers check first. Before starting a download, the page asks the API for the download links (a small, normal JSON request). If that fails with 403 or 404, the page shows a proper message. Only when it succeeds does it start the native download. The pre-flight costs one small request and turns "nothing happened" into a sentence the user can act on.

Takeaways

  • response.blob() holds the whole file in memory. For anything large, let the browser's download manager handle it.
  • A security rule that blocks you is usually doing its job. Look for a way that fits the rule before changing the rule.
  • Silent failures are the worst kind. Framing refusals don't throw, so a broken download looked like a button that did nothing.
  • Pre-flight what you can't observe. A cheap request first lets the page report errors that a native download would swallow.
  • When a feature crosses origins, check both sides' headers. The page's policy and the API's policy each had a veto.

Found this useful?

Built something with what you learned here, or spotted a better way? Tell us. We work with freelance developers on Shootlo.