My Unity build unzipped itself for a minute because of one missing header
A 58 MB Unity build decoded itself in JavaScript for a full minute. The fix was one header. The test that lied to me was curl.
There is a particular kind of minute that only exists in web game development. It is the minute between clicking Play and getting to play.
Ours was about sixty seconds long, which is a lot for a minute. During it, a 58 MB Unity build sat in a browser tab and decompressed itself. In JavaScript. One byte at a time, like a man unpacking a suitcase while explaining each sock.
Things you can do in that minute, which I have personally tested:
- Make tea, as long as you don't drink it
- Read the loading bar's percentage aloud like it owes you money
- Wonder whether anyone has ever waited this long for a game called Bad anything
- Close the tab, which is what actual players do
This is the third day running that I've written about a header, so let's just accept that this is a header column now.
Unity has a plan B, and it was running the whole time
When you build Unity for the web with Brotli compression, the files come out compressed. Somebody has to decompress them before the game can run. There are two candidates.
The browser. Browsers have native Brotli decoders, written in fast compiled code, and they use them on any response that arrives with Content-Encoding: br. The decompression happens while the bytes stream in, and nobody notices.
Unity's loader. If you tick "Decompression Fallback" in the publishing settings, Unity ships its own decompressor in JavaScript. The loader downloads the compressed file, then grinds through it by hand. It works everywhere, which is the point of a fallback. It is also much slower, which is the price of a fallback.
Ours was taking plan B. Not because the browser couldn't decompress, but because nobody told it to. The server sent the files with no encoding header at all, so the browser handed over a blob of compressed bytes, Unity's loader shrugged, and the JavaScript decoder started doing its sock routine.
There is a second cost that's easier to miss. A WebAssembly module that arrives as a proper application/wasm response, already decompressed by the browser, can go through WebAssembly.instantiateStreaming, which compiles the code while it downloads. A file that JavaScript has to decompress first can't. So we weren't only decompressing slowly - we were also compiling later, in series, after the slow part was done.
Where my evening went
The file extension is the contract
Here is the rule I wish someone had tattooed on my forearm:
.wasm.br,.data.br,.framework.js.brmean "the server will sendContent-Encoding: br, and the browser will do the work.".unitywebmeans "the server will send nothing special, and the JavaScript will do the work."
Unity names the files according to which deal you picked. The server's job is to honour the deal. A file ending in .br should leave with Content-Encoding: br and its real content type underneath - application/wasm for the wasm, application/javascript for the framework script. If the headers and the extension disagree, one side is lying, and the browser always believes the headers.
Practical version: turn Decompression Fallback off, so you get .br files. Make the server label them correctly. Then, and only then, check that it worked, which brings me to the part where I wasted an evening.
Brotli also only gets advertised by browsers over HTTPS, so if you're testing on plain HTTP on a local network, the browser won't ask for it in the first place. Another evening I'm not getting back.
The test that lied to me
The natural way to check compression from a terminal is curl --compressed. It asks the server for compressed content and decompresses whatever comes back. Lovely.
Except on many curl builds, including the one macOS ships, Brotli isn't compiled in. Ask it to handle a Brotli response and it silently returns zero bytes. No error worth the name. Just an empty file, which looks exactly like a broken server.
So I'd fix the server, run curl, see nothing, conclude the server was still broken, and "fix" it again. I was decompressing all week, just not the way I meant.
Check your curl first:
curl -V | grep -i brotli
If nothing comes back, your curl can't read Brotli, and --compressed will gaslight you. Don't decompress at all - just look at the headers:
curl -sI -H 'Accept-Encoding: br' https://example.com/Build/game.wasm.br
What you want to see in that response:
content-encoding: brcontent-type: application/wasm(for the wasm file)- A status of 200, not a redirect to somewhere that forgot all of the above
Then open the browser's network panel, click the wasm request, and confirm the same thing there. The browser is the only client whose opinion matters, and it has a Brotli decoder.
What I'd tell myself a week ago
The fallback isn't a bug. It's a seatbelt for servers you don't control. If you do control the server, it's a seatbelt you're wearing in the living room.
The real lesson is broader than Unity: compression is a handshake between the file name, the server's headers and the client's abilities, and every testing tool you use is a fourth party with its own opinions. When something returns nothing, ask first whether the tool can read what it's being handed.
After the fix, the browser does the decoding while the bytes arrive and the wasm compiles as it streams. The tea, unfortunately, still takes a minute.