Bad premises. Good times.

No downloads, no installs, no good reason. Just play.

Quick tourney Aqua Strike begins in · 4 of 8 in

Daily Play

Daily Play · · Building games · 4 min read

My loading bar sat at 99% because it trusted Content-Length

The browser decompresses Unity files before your progress code sees them. Our wasm grew 5.4 times on arrival, and the bar gave up.

If you've been reading this week, yes, it's compression again. I'm sorry. Some families go camping. Mine watches me argue with brotli at 1am.

Today's problem is the loading bar that climbs briskly to 99% and then parks there, engine off, like a dad at a rest stop who said "five minutes" forty minutes ago.

Every player has seen this bar. Every player assumes the game is broken. Most of them leave. The ones who stay are the same people who believe "we're nearly there" from the front seat, and I love them, but I can't build a business on them.

Where the bytes go missing

Here's the setup. You ship your Unity WebGL build brotli-compressed, because a 52 MB wasm file is rude. The server sends it with Content-Encoding: br and a Content-Length header.

Two facts, which turn out to disagree:

  • Content-Length counts the compressed bytes. That's what actually crossed the wire.
  • The browser decodes Content-Encoding transparently. By the time your loader is counting bytes, they've already been decompressed. It counts the big number, not the small one.

So your progress maths is dividing decompressed bytes by a compressed total. That's apples over very small apples.

Unity knows this happens, so its loader guesses. For brotli it assumes the file roughly doubles when decoded. A reasonable guess. Like assuming a toddler will eat roughly half their dinner.

Here's what our files actually did:

Unity build files: on the wire vs decoded

Compressed (brotli)Decoded

The loader guessed 2x. The wasm went 5.4x, the data went 1.4x. Nobody guessed right, least of all the loader. Source: badgames.io, measured
  • The wasm: 9.6 MB on the wire, 52 MB decoded. That's 5.4x, not 2x.
  • The data file: 51.7 MB on the wire, 72.4 MB decoded. That's about 1.4x.

So the guess was wrong in both directions at once, which takes real talent.

The wasm is the one that hurts. If the loader expects about 19 MB of decoded bytes and 52 MB turn up, it hits its "full" mark when barely a third of the file has really arrived. The bar can't go past 100, so it pins itself just below and waits. Then the next 33 MB arrive with no visible change. That's the 99% parking spot. That's the rest stop.

The data file goes the other way. It finishes under the estimate, so its part of the bar jumps to done at the end instead of creeping. Less painful, still a lie.

Compiled code compresses brilliantly, because it's mostly the same instructions repeating. Your textures and audio are usually compressed already, so brotli has little to add. A single 2x guess can't be right for both, and the more code-heavy your build, the worse the wasm guess gets.

Jumper game art with the top line 'Loading: 99%' and the bottom line 'Still 99%. Since the kettle boiled.'
The last percent is the one that teaches patience. And sends players to another tab.

The fix is to stop guessing

The browser hides the decoded size from you until the bytes are already decoded. But you know it before you ever upload. You're the one who compressed the file.

So our publisher, the script that pushes a build live, now decompresses each file once at publish time, measures it, and writes the real sizes into a tiny JSON file next to the build. Here's the idea, cut down:

import { brotliDecompressSync } from 'node:zlib';
import { readFileSync, writeFileSync } from 'node:fs';

const sizes = {};
for (const f of ['game.wasm.br', 'game.data.br']) {
  const raw = brotliDecompressSync(readFileSync(`Build/${f}`));
  sizes[f.replace('.br', '')] = raw.length;
}
writeFileSync('Build/sizes.json', JSON.stringify(sizes));

The page fetches that file first. It's a few dozen bytes, so it costs nothing. Then progress is just decoded bytes received divided by the sum of the real decoded sizes. Same units on top and bottom. Maths, as my kids keep reminding me, is easier when you do it properly.

The result: the bar moves at the speed the download actually moves. It reaches 100% when the files are there, not a third of the way through.

Rules I'm keeping

A short list, for the next tired person:

  1. Never trust Content-Length as a progress total when a response is content-encoded. It's counting a different thing from what your loader receives.
  2. Don't use one ratio for every file. Ours ranged from 1.4x to 5.4x in the same build.
  3. Measure at build time, not at runtime. The publisher has the files, the time and nothing better to do.
  4. Ship the numbers next to the build. If the JSON file is missing, fall back to the old guess. A wrong bar beats a broken page.
  5. Watch your own loading screen on a slow connection. It feels very different on a train with one bar of signal.

Where the player's time goes on a lying loading bar

Time is relative. Mostly relative to the number one below 100. Not real data. Painfully accurate, though.

I spent an evening on this and my first reaction was that it's cosmetic. The game loads at the same speed either way. Nobody's frame rate improves.

But a loading bar is the first promise a game makes. If it lies in the first ten seconds, the player has no reason to believe your leaderboard, your tournament timer or anything else you tell them. Honesty starts at 0%.

So I had to raise the bar. Sorry. I warned you there would be one pun and I've used it up on that.

The bar now reaches 100%. Nobody is waiting at the rest stop. Next week someone in the back seat will ask "are we there yet," and for once the screen will be right.

unitywebglcompressionloading barsbuilding games