Bad ideas. Great games.

Silly games, taken way too seriously. Just hit play.

Quick tourney Aqua Strike begins in · 4 of 8 in

Daily Play

Daily Play · · Building games · 4 min read

I told browsers to cache Unity files for a year. They listened.

One cache header, three default file names, and a Unity build that crashes when new data meets old code. The fix is one checkbox.

There is a particular kind of tired that comes from discovering a bug you cannot fully fix, only stop making worse. This is one of those. It involves a cache header I was proud of and a Unity build I didn't look at closely enough.

The header that does exactly what it says

Static files that never change should be cached hard. That is standard advice, and it is good advice. So for game engine files, the server sent this:

Cache-Control: max-age=31536000, immutable

31,536,000 seconds is 365 days. immutable tells the browser not to bother checking back, not even on a reload. You are making a promise: this URL will always serve these exact bytes. The browser believes you, because browsers are trusting in a way that people who ship software should never be.

The promise only holds if the file name changes whenever the content does. Webpack and Vite do this for you by stamping a hash into every filename. I had that pattern in my head. I assumed the Unity build did the same.

It did not. The first Unity build went out as:

WebGL.data.br
WebGL.wasm.br
WebGL.framework.js.br

Those are Unity's default names, the ones you get when you don't change anything. Every player who loaded that build now has those three URLs pinned in their browser for a year. "Immutable" turned out to describe my mistake rather than the files.

Meme over Aqua Strike art: cache it for a year, what could possibly change
The build. The build could change.

Worse than stale

If that were the whole story, it would be a stale-game story. A player sees last month's version, shrugs, and the world goes on. Annoying, survivable.

It is worse, and the reason is the most useful thing in this column if you ship Unity to the web.

Unity's WebGL loader treats its files differently. The data file, the big one with your assets and scenes, goes through Unity's own cache in IndexedDB, and Unity revalidates it. The code, meaning the wasm and the framework JavaScript, comes straight from the ordinary HTTP cache. The same HTTP cache I had just told to hold everything for a year without asking.

So when I published a new build under the same three names, a returning player got this:

  • Data: new, because Unity checked and fetched the update.
  • Code: old, because the browser had been told never to check.

New data with old code doesn't give you an old game. It gives you a game that crashes, because the engine code is reading assets built for a different version of itself. From the player's side it doesn't look like "you're on an old version". It looks like the game is broken, which, to be fair, it was.

The partial cache was the trap. If everything had been stale, the build would at least have matched itself. Half-fresh is the dangerous state.

Rows of drawers
Artist's impression of a browser cache. Things go in. Nothing comes out for 365 days. Photo: Sue Peacock, CC BY-SA 2.0, Wikimedia Commons

The fix is one checkbox. The cleanup is a calendar.

In Unity: Player Settings -> Publishing Settings -> Name Files As Hashes. Tick it. The build files get named after a hash of their contents, so new content means a new URL, and a new URL means the year-long cache can't hand back the old one. That's the whole fix for future builds. One checkbox. I have spent longer choosing a font colour.

Checkboxes get unticked, though. Someone starts a new project, or a template resets, and the default comes back. So the second change matters more than the first: the publisher now refuses unhashed builds. If the data, wasm or framework file is still called something like WebGL.data.br, the upload stops with an error instead of shipping. The check is dull, roughly this shape:

// sketch: reject build files that aren't content-named
const buildFile = /\.(data|wasm|framework\.js)(\.br|\.gz)?$/;
for (const name of files.filter(f => buildFile.test(f))) {
  if (name.startsWith('WebGL.')) {
    throw new Error(`Unhashed Unity build file: ${name}. Tick "Name Files As Hashes".`);
  }
}

Match whatever your default product name is. The point is that the rule lives in the tool, not in my memory, because my memory is currently busy with a school bake sale.

The third piece is the loader script. It keeps a fixed name, so it gets requested with a query string: ?v=<data file name>. Because the data file's name is now a hash, the loader's URL changes every time the build does, and the browser fetches a matching loader instead of reusing an old one that expects files that are gone.

Now for the part I can't fix. The players who loaded that first build still have those three default URLs pinned. Nothing I send from the server reaches them, because the whole point of immutable is that the browser doesn't ask. I can't change a header I already sent to someone else's machine. The only thing that frees them is never requesting those URLs again, so that the old cached copies sit unused until the year runs out. New names are the only way out. Call it a cache-22, then put the pun down and back away from it slowly.

How long my mistake lasts

One of these bars is doing a lot of work. Not real data. Painfully accurate, though.

What to take from this

If you ship Unity WebGL, or anything else with long cache headers:

  1. Long cache lifetimes require content-addressed names. It's a pair. A year-long cache on a fixed filename is a bug you've scheduled for later.
  2. Know which cache each file goes through. Unity revalidates the data but not the code. When two parts of a build are cached differently, they can drift out of sync, and mismatched versions crash where a fully stale build would only be out of date.
  3. Put the rule in the publishing tool. A setting you have to remember is a setting you will eventually forget. A publisher that refuses the bad build never forgets.
  4. Assume you can't take a header back. Before you send immutable, imagine needing to undo it. You can't. The best you can do is stop pointing at the old URLs.

The new builds load cleanly. The old URLs are still sitting in some browsers, waiting out their year. I now read every Unity build folder before it ships, and it is not a fun hobby.

unitywebglcachinghttpbuilding games