Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(devex): pre-compress static files at collectstatic time (#70422)

Julian Bez53b905b7e230
diff --git a/Dockerfile b/Dockerfile
index cf271ab8ee7e..a099bd1bd6f9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -195,7 +195,7 @@ RUN SKIP_SERVICE_VERSION_REQUIREMENTS=1 STATIC_COLLECTION=1 DATABASE_URL='postgr
COPY --from=sourcemap-upload /tmp/.sourcemaps-status /tmp/.sourcemaps-status
RUN if [ "$(cat /tmp/.sourcemaps-status)" = uploaded ]; then \
echo "sourcemaps uploaded — stripping .map files from the image"; \
- find /code/staticfiles /code/frontend/dist -name '*.map' -delete; \
+ find /code/staticfiles /code/frontend/dist \( -name '*.map' -o -name '*.map.gz' -o -name '*.map.br' \) -delete; \
else \
echo "sourcemaps NOT uploaded — retaining .map files in the image"; \
fi
diff --git a/posthog/settings/web.py b/posthog/settings/web.py
index 8e4d5ad880ad..bfeef5858219 100644
--- a/posthog/settings/web.py
+++ b/posthog/settings/web.py
@@ -4,6 +4,7 @@ from datetime import timedelta
import structlog
from corsheaders.defaults import default_headers
+from whitenoise.compress import Compressor
from posthog.scopes import get_scope_descriptions
from posthog.settings.base_variables import BASE_DIR, CLOUD_DEPLOYMENT, DEBUG, TEST
@@ -405,13 +406,23 @@ STORAGES = {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
+ # CompressedManifest: collectstatic pre-generates .br and .gz (brotli is
+ # already a dependency) so WhiteNoise serves compressed bytes from disk and
+ # the envoy edge — which otherwise gzips static per request (Contour's
+ # default compression filter) — skips recompression since Content-Encoding
+ # is already set. Same Manifest base class: hashed names unchanged.
"BACKEND": (
"django.contrib.staticfiles.storage.StaticFilesStorage"
if TEST
- else "whitenoise.storage.ManifestStaticFilesStorage"
+ else "whitenoise.storage.CompressedManifestStaticFilesStorage"
),
},
}
+# Never emit .map.gz/.map.br: the production image deletes *.map after the
+# sourcemap upload, and compressed variants would survive that cleanup and
+# leak the maps under /static. Also skips pointless build-time compression
+# of files that get deleted anyway.
+WHITENOISE_SKIP_COMPRESS_EXTENSIONS = [*Compressor.SKIP_COMPRESS_EXTENSIONS, "map"]
def static_varies_origin(headers, path, url):