diff --git a/frappe/app.py b/frappe/app.py index 110e5262e4..f5c5b69f88 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -236,25 +236,17 @@ def log_request(request, response): ) -def process_response(response): +def process_response(response: Response): if not response: return # cache control # read: https://simonhearne.com/2022/caching-header-best-practices/ if frappe.local.response.can_cache: - response.headers.extend( - { - # default: 5m (client), 3h (allow stale resources for this long if upstream is down) - "Cache-Control": "private,max-age=300,stale-while-revalidate=10800", - } - ) + # default: 5m (client), 3h (allow stale resources for this long if upstream is down) + response.headers.setdefault("Cache-Control", "private,max-age=300,stale-while-revalidate=10800") else: - response.headers.extend( - { - "Cache-Control": "no-store,no-cache,must-revalidate,max-age=0", - } - ) + response.headers.setdefault("Cache-Control", "no-store,no-cache,must-revalidate,max-age=0") # Set cookies, only if response is non-cacheable to avoid proxy cache invalidation if hasattr(frappe.local, "cookie_manager") and not frappe.local.response.can_cache: diff --git a/frappe/utils/response.py b/frappe/utils/response.py index fb63065ebc..a9fb61e3ef 100644 --- a/frappe/utils/response.py +++ b/frappe/utils/response.py @@ -293,6 +293,7 @@ def send_private_file(path: str) -> Response: path = "/protected/" + path response = Response() response.headers["X-Accel-Redirect"] = quote(frappe.utils.encode(path)) + response.headers["Cache-Control"] = "private,max-age=3600,stale-while-revalidate=86400" else: filepath = frappe.utils.get_site_path(path)