Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure HTML pages vary on HX-Request #112

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"django_htmx.middleware.HtmxMiddleware",
"general.middleware.ExtraVaryMiddleware",
]

if DEBUG and DEBUG_TOOLBAR:
Expand Down
17 changes: 17 additions & 0 deletions app/general/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.utils.cache import patch_vary_headers


class ExtraVaryMiddleware:
"""Ensure HTML pages vary on HX-Request

This is needed so that incomplete responses based on base_htmx.html are not
reused as full-page responses, for example on browser restore of a page."""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if "text/html" in response.headers["Content-Type"]:
patch_vary_headers(response, ["HX-Request"])
return response
Loading