My 3D models shipped uncompressed for months over one missing MIME type
Gzip was switched on the whole time. It just never knew what a .glb was. The fix saved 40%, and the uneven numbers taught me more.
Yesterday I told you about telling browsers to cache Unity files for a year. Today's confession is the same kind of thing: something I was sure was working, that was not working, and had not been working for months.
Every 3D model on the site went out uncompressed. Not some of them. All of them. Gzip was on. The config had a nice tidy list of things to compress. I had looked at that list. I had, I think, nodded at it.
None of that mattered, because the server had no idea what a .glb file was.
Gzip does not read file extensions
This is the bit worth tattooing somewhere. Most web servers decide what to compress by looking at the response's Content-Type, not the file's name. The rule is "compress things that are text/css, application/javascript, and so on." It is not "compress things ending in .css."
So the chain goes like this. A browser asks for walk.glb. The server looks up .glb in its table of known extensions, finds nothing, and falls back to a generic binary type, usually application/octet-stream. That is the server's polite way of saying "some bytes, no idea." Then the compression step checks its list, does not see that generic type, and sends the file as is.
You can add glb to the compression list as many times as you like. It will never match, because the list is matching on types, and the file never got a proper one. An unknown extension cannot be compressed, whatever the list says.
The model was fine. It just was not the server's type.
The fix needs two things to be true, whatever server you run:
- The
.glbextension maps to its real type,model/gltf-binary. - That type is on the list of things to compress.
Miss the first and the second is decoration. That is what I had. Decoration.
The way to check takes ten seconds, and I should have run it months ago:
curl -sI -H 'Accept-Encoding: gzip' https://your-site/models/walk.glb \
| grep -iE 'content-(type|encoding)'
If you see application/octet-stream and no Content-Encoding: gzip, you have my problem. If you see model/gltf-binary and gzip, you are doing better than I was, and you may feel briefly smug.
The numbers, and why they disagree
Once the type was set, here is what three of our models did:
| Model | Before | After | Saved |
|---|---|---|---|
| walk.glb | 281 KB | 106 KB | 62% |
| thrower.glb | 554 KB | 337 KB | 39% |
| lifeguard.glb | 2.05 MB | 1.30 MB | 36% |
Together that is about 2.89 MB down to 1.74 MB, roughly 1.1 MB nobody has to download any more, on every cold load, for free. One line of config. I am choosing not to work out how many players paid for my mistake in mobile data.
Same gzip, very different savings
BeforeAfter gzip
The total is nice. The spread is the interesting part. Why would one model shrink by 62% and another by 36%, from the same algorithm with the same settings?
Because a .glb is not one kind of data. It is a container holding two very different things: geometry and textures.
Geometry compresses hard. Vertex positions are stored as raw float32 numbers. A float32 is a sign bit, an 8-bit exponent and a 23-bit fraction. In a single model, the vertices all sit in a similar range, a character a couple of units tall centred near the origin. So a huge share of those numbers have the same or nearly the same exponent, and the same few bytes turn up again and again. Repetition is what gzip eats for breakfast. A model that is mostly mesh, like the walk animation, is a gzip buffet.
Textures barely move. The images inside a .glb are usually JPEGs or PNGs, and those are already compressed. Compressing a compressed file is like vacuum-packing a vacuum pack. You get a bit of air out of the wrapper and that is it. The two models that saved less are the ones carrying the most image data, so most of their bytes were already as small as gzip could make them.
The free diagnostic hiding in your gzip ratio
Here is the bit I actually want you to take away, because it changes what you do next.
The usual advice for heavy 3D models on the web is "use Draco." Draco is mesh compression. It is very good at geometry. It does nothing for textures.
Gzip, it turns out, has just told you how much of each model is geometry. A model that gzips really well is mostly mesh, and Draco has plenty to work on. A model that gzips badly is mostly already-compressed images, and Draco will not help much either, because the part it compresses is not where the weight is.
So before you add a decoder to your build and an extra decode step to your load time, run gzip on the file and look at the ratio. It costs nothing:
- High ratio, like walk.glb: geometry-heavy. Draco is worth trying.
- Low ratio, like lifeguard.glb: texture-heavy. Leave the mesh alone and look at the images. Are they bigger than they will ever be drawn on screen? A 2048 texture on a character that fills a quarter of a phone screen is paying for pixels nobody sees.
I like this because it turns a vague "models are big, compress them" into an actual decision, with a number behind it, before you write any code.
My position, for what it is worth: do not reach for the clever tool first. Get the boring part right - correct MIME type, gzip actually running, verified with curl rather than with faith - and then let the boring part tell you whether the clever tool is worth the trouble. Most of the time the answer is "shrink your textures," which is less exciting than a compression library and much more effective.
And check your headers. Not your config. Your headers. The config is what you think you told the server. The headers are what it is actually doing. For months, those were two very different stories, and only one of them was being downloaded.