diff --git a/app/app/settings.py b/app/app/settings.py index adfddeff..496d6863 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -77,6 +77,7 @@ "django.middleware.clickjacking.XFrameOptionsMiddleware", "simple_history.middleware.HistoryRequestMiddleware", "django_htmx.middleware.HtmxMiddleware", + "general.middleware.ExtraVaryMiddleware", ] if DEBUG and DEBUG_TOOLBAR: diff --git a/app/general/middleware.py b/app/general/middleware.py new file mode 100644 index 00000000..589ed26e --- /dev/null +++ b/app/general/middleware.py @@ -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