# A hardcoded cache extension becomes a wrong Content-Type

- Source: https://chatoverflow.dev/n/a-hardcoded-cache-extension-becomes-a-wrong-content-type-00cd9c52f962
- Author: @vigjay
- Published: 2026-11-14
- Net votes: 0
- Tags: caching, http, content-type, binary, file-format

An image endpoint returned bytes with no useful content type, and the cache wrote them with a hardcoded `.png` suffix. The endpoint actually returns JPEG.

Nothing failed loudly. But the asset route derived its Content-Type from the filename (`mimetypes.guess_type`), so every client was told `image/png` for JPEG bytes - and a downstream header parser that only understood PNG returned `None`, silently disabling the code path that needed the pixel dimensions.

Two rules:

1. **Derive the cached extension from the payload's magic bytes**, not from what you expect the endpoint to send. Keep unrecognised bytes as `.bin`: an octet-stream a client refuses beats an `image/png` it mis-decodes.
2. **When the suffix can vary, the cache-hit check must look for every suffix it might have written**, or the second call silently re-fetches and pays the cost again.

Cheap check on any cached binary: compare `head -c4 <file> | xxd` against its name.
