Case file 06Frontend · Performance8 min read

The Excel export that moved out of the browser twice

One feature rebuilt three times in five months: main thread, Web Worker, then the server.

Read by an AI voice of Sajib. Words light up as he reads.

I clicked the new “Download all projects” button and waited. The loading spinner on the button did not spin. Nothing on the page moved. To any user, that tab looked crashed.

It was actually busy building a large Excel file, with no time left to draw anything on the screen.

This article follows one feature that was rebuilt three times in five months. It started on the browser’s main thread, moved into a Web Worker, and finally moved to the server. Each move says something about where heavy work should live.

Where the export came from

The product is an analytics tool built for a client. Teams upload creative work into projects, the system analyses it, and users read the results as scores and written findings.

Each project already had its own Excel download. It started as a simple CSV file. Over time it became a styled Excel file with colours, merged cells and separate sections. I used a fork of SheetJS, a JavaScript library that can write Excel files, because the fork can also write cell styles.

That file grew to over a thousand lines in one module. So I split it into small pieces: one builder per section, some functions that reshape the data from the API, and shared style files. That refactor paid off, because the next request reused almost all of it.

Then a new request came in. Admins on some customer accounts wanted one Excel file that covered every project. It needed the same styled sections for each project, plus a second sheet listing every analysed document.

Version 1: build it in the browser

The first version was the obvious one. A button in the sidebar asked the user to confirm. Then the browser fetched every project as JSON, looped over them, added each project’s sections to one big sheet, styled every cell, added the document sheet, and asked SheetJS to write the file.

All of that ran on the main thread. The main thread is the one thread where the browser runs your JavaScript and also draws the page. While your code is running, the browser cannot draw anything.

For a few projects, the export was instant. For a real customer account, it was not. The loop, the styling of every cell and the final file writing all blocked the main thread together. That is why the spinner froze.

Fig. 6.1 · While the export ran on the main thread, the browser could not draw anything.

I thought about splitting the loop into small pieces with setTimeout, so the browser could draw between pieces. But the builder was not written in steps that could pause and continue. And the final step, writing the file, is one big step that can’t be split. So three days after the first version, I moved the work somewhere else.

Version 2: the same builder, on a different thread

A Web Worker is a separate thread in the browser that can run JavaScript without blocking the page. It can’t touch the page directly; it talks to the main thread by sending messages with postMessage.

The builder was already a pure function: data goes in, a workbook comes out, and nothing touches the page. That made the move small. I changed the top function from “build and start the download” to “build and return the file as raw bytes”, and I wrapped it in a worker.

A few details mattered:

  • The data goes into the worker as a full copy. postMessage copies whatever you send. I accepted that cost.
  • The finished file comes back as a “transferable” object. That means the bytes move to the main thread without a second copy.
  • I create one worker per export and stop it when the export is done or fails. No worker stays around holding a lot of memory between exports.

To test it, I made a large sample file with 348 realistic projects, about 4.6 MB of data. I pointed the worker at it while tuning. This is also where I made my first mistake: for one commit, the worker was building from the sample file instead of the real data. I found and fixed it the next day. The sample file is still in the repository, which I would also undo now.

Progress that means something

A spinner is not enough for a build that can take a while. So the next day I added progress. The builder takes an optional callback function, and the worker forwards each call to the main thread as a message.

Here is the idea, simplified. There are two kinds of message: progress updates while it works, and one final message with either the file or an error.

self.onmessage = ({ data }) => {
  try {
    const file = buildWorkbook(data, (phase, done, total) =>
      self.postMessage({ kind: "progress", phase, done, total })
    );
    // the second argument transfers the bytes instead of copying them
    self.postMessage({ kind: "done", ok: true, file }, [file]);
  } catch (e) {
    self.postMessage({ kind: "done", ok: false, error: String(e) });
  }
};

The build had three real stages: the project tables, the document sheet, and the styling. The styling stage can touch tens of thousands of cells, so it reports only every 100 cells. If it reported every cell, sending the messages would cost more than the work itself. The window showed a label like “Processing project 41/348” and a progress bar.

That bar was less good than it looked. Each stage had its own total, so the bar filled to 100% and then dropped back to zero at the start of the next stage. And the final file writing, one of the heaviest steps, could not report progress at all. The bar just waited there while the real work happened. Users read a full bar as “done”. It was honest about stages, but not about time.

Real data broke it in a different way

A few months later, the export started failing for real customers, for reasons that had nothing to do with threads.

One field, the document description, was sometimes a JSON string and sometimes plain text. My code always tried to read it as JSON, so plain text caused errors. I added a quick check before reading it. When it is JSON, I turn it into readable labelled lines.

The other problem was Excel’s own limit. One cell can hold at most 32,767 characters. Some descriptions were long HTML emails. I added a step that removes the HTML and cuts the text at 25,000 characters, with a visible “(continued)” marker at the end.

My own sample file caught neither problem, because both came from the shape of real production data.

Version 3: stop building it in the browser

The worker fixed the frozen tab. It did not fix the real cost, which grew as customers grew:

  1. The browser downloaded the whole account’s data as JSON before anything started.
  2. postMessage copied all of it into the worker.
  3. SheetJS held the whole workbook in memory, then wrote it in one step.

So at the peak, the admin’s laptop held the data twice plus the workbook. There was also a new request that no browser could meet. The client wanted a copy of the report sent by email. That means the file has to exist somewhere other than the user’s tab.

The backend team built the server-side export. The frontend’s job was to switch over, and to design the experience for a request that might take minutes.

The frontend makes one request and waits for a file, with a timeout of 15 minutes. When the file arrives, the browser starts the download. The waiting window says plainly that large exports can take up to 15 minutes, that the user can close the window while the download continues, and that a copy will also arrive by email.

“Tracking the job” here is simpler than it sounds. There is no job ID and no polling. The frontend just keeps one long HTTP request open. Closing the window only hides it. The request keeps running, because the button lives in the sidebar, which stays on screen while the user moves around the app. If the user closes the tab, the request stops, and the email copy is the backup.

That was a deliberate choice. It needed no new job system on the frontend or the backend, and the email covered the failure case.

Fig. 6.2 · Each version moved the heavy work further away from the user's tab.

The old worker code is still in the codebase, but the button no longer uses it.

One more reuse, and one revert

Two weeks later, I added a filtered version of the report, by month, quarter or year, using the same server export. My first attempt put the download in the project filter bar. I reverted it the next day. Then I added it to a dashboard chart section instead, with checks that stop invalid filter combinations before the request is sent.

What is still missing

I didn’t keep timing measurements from the browser versions. So I can’t say how many seconds an export took on the main thread compared with the worker. I only know how long each version lasted: the first one three days, the second about four months.

What I learned

Write the expensive part as a pure function first. Because the builder took data and returned a file, moving it into a worker took about a day. Moving the work to the server didn’t touch the builder at all.

A worker fixes responsiveness, not cost. The page stops freezing, but the data still has to reach the browser and still gets copied. If the input grows with the customer, plan the server path early.

Measure before you move things. I moved the build into a worker because “it freezes”. That was the right call. But if I had recorded the build time and memory for the large sample, I could have made the case for the server months earlier.

A long request can be enough. Not every slow task needs a job queue and polling. One request with a generous timeout and a second way to deliver the file was the smallest thing that worked.

Product names and some details are changed or left out. The engineering is real.