refactor: guard clause before image caching logic

This commit is contained in:
Arjan Singh 2025-08-29 12:57:18 -07:00
parent 7cd68913d4
commit 8544db8667
No known key found for this signature in database
GPG Key ID: B1102A5F9699979D

View File

@ -32,15 +32,21 @@ async def cache_control(
or request.path.endswith("index.json") or request.path.endswith("index.json")
): ):
response.headers.setdefault("Cache-Control", "no-cache") response.headers.setdefault("Cache-Control", "no-cache")
elif request.path.lower().endswith(IMG_EXTENSIONS): return response
if response.status == 404:
response.headers.setdefault("Cache-Control", f"public, max-age={ONE_HOUR}") # Early return for non-image files - no cache headers needed
elif response.status in (200, 201, 202, 203, 204, 205, 206, 301, 308): if not request.path.lower().endswith(IMG_EXTENSIONS):
# Success responses and permanent redirects - cache for 1 day return response
response.headers.setdefault("Cache-Control", f"public, max-age={ONE_DAY}")
elif response.status in (302, 303, 307): # Handle image files
# Temporary redirects - no cache if response.status == 404:
response.headers.setdefault("Cache-Control", "no-cache") response.headers.setdefault("Cache-Control", f"public, max-age={ONE_HOUR}")
# Note: 304 Not Modified falls through - no cache headers set elif response.status in (200, 201, 202, 203, 204, 205, 206, 301, 308):
# Success responses and permanent redirects - cache for 1 day
response.headers.setdefault("Cache-Control", f"public, max-age={ONE_DAY}")
elif response.status in (302, 303, 307):
# Temporary redirects - no cache
response.headers.setdefault("Cache-Control", "no-cache")
# Note: 304 Not Modified falls through - no cache headers set
return response return response