Skip to content

Commit

Permalink
Test no cache for ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
smdsgn committed Sep 16, 2024
1 parent b191eca commit 388b11b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from csp.middleware import CSPMiddleware
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.utils.cache import add_never_cache_headers
from django_app_parameter import app_parameter

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -66,3 +67,17 @@ def __call__(self, request):
request.htmx = "HX-Request" in request.headers
response = self.get_response(request)
return response


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

def __call__(self, request):
response = self.get_response(request)

# Désactive le cache pour les requêtes AJAX
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
add_never_cache_headers(response)

return response
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"config.middlewares.HtmxMiddleware",
"config.middlewares.LogIncomingRequest",
"config.middlewares.MaintenanceModeMiddleware",
"config.middlewares.NoCacheForAjaxMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down

0 comments on commit 388b11b

Please sign in to comment.