Skip to content

Commit 346b142

Browse files
grichaclaude
andcommitted
fix(middleware): Skip viewer context for static asset routes
Avoid accessing request.user for ANONYMOUS_STATIC_PREFIXES paths, which would add Vary: Cookie and break HTTP caching for static assets. This matches the pattern used by StaffMiddleware and SuperuserMiddleware. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 15cd6d8 commit 346b142

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/sentry/middleware/viewer_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from collections.abc import Callable
44

5+
from django.conf import settings
56
from django.http.request import HttpRequest
67
from django.http.response import HttpResponseBase
78

@@ -26,6 +27,12 @@ def ViewerContextMiddleware_impl(request: HttpRequest) -> HttpResponseBase:
2627
if not enabled:
2728
return get_response(request)
2829

30+
# This avoids touching user session, which means we avoid
31+
# setting `Vary: Cookie` as a response header which will
32+
# break HTTP caching entirely.
33+
if request.path_info.startswith(settings.ANONYMOUS_STATIC_PREFIXES):
34+
return get_response(request)
35+
2936
ctx = _viewer_context_from_request(request)
3037
with viewer_context_scope(ctx):
3138
return get_response(request)

0 commit comments

Comments
 (0)