From 79ca9c696332aa4fcbef42b83ba588b2401fc1fc Mon Sep 17 00:00:00 2001 From: Dave Gaeddert Date: Sun, 21 Jan 2024 16:01:25 -0600 Subject: [PATCH] Use Vary header in htmx views to fix caching issues (back button) --- bolt-htmx/bolt/htmx/views.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bolt-htmx/bolt/htmx/views.py b/bolt-htmx/bolt/htmx/views.py index b9e6929494..99c5335b23 100644 --- a/bolt-htmx/bolt/htmx/views.py +++ b/bolt-htmx/bolt/htmx/views.py @@ -1,5 +1,7 @@ import re +from bolt.utils.cache import patch_vary_headers + class HTMXViewMixin: htmx_template_name = "" @@ -19,6 +21,13 @@ def render_template(self): return template.render(context) + def get_response(self): + response = super().get_response() + # Tell browser caching to also consider the fragment header, + # not just the url/cookie. + patch_vary_headers(response, ["HX-Request", "BHX-Fragment", "BHX-Action"]) + return response + def get_request_handler(self): if self.is_htmx_request: # You can use an htmx_{method} method on views