Skip to content

Commit

Permalink
Use Vary header in htmx views to fix caching issues (back button)
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 21, 2024
1 parent 990daf7 commit 79ca9c6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bolt-htmx/bolt/htmx/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

from bolt.utils.cache import patch_vary_headers


class HTMXViewMixin:
htmx_template_name = ""
Expand All @@ -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
Expand Down

0 comments on commit 79ca9c6

Please sign in to comment.