mirror of
				https://git.sr.ht/~tsileo/microblog.pub
				synced 2025-06-05 21:59:23 +02:00 
			
		
		
		
	Improve static assets caching
This commit is contained in:
		
							
								
								
									
										20
									
								
								app/main.py
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								app/main.py
									
									
									
									
									
								
							| @@ -119,7 +119,7 @@ class CustomMiddleware: | ||||
|                 # And add the security headers | ||||
|                 headers = MutableHeaders(scope=message) | ||||
|                 headers["X-Request-ID"] = request_id | ||||
|                 headers["Server"] = "microblogpub" | ||||
|                 headers["x-powered-by"] = "microblogpub" | ||||
|                 headers[ | ||||
|                     "referrer-policy" | ||||
|                 ] = "no-referrer, strict-origin-when-cross-origin" | ||||
| @@ -910,6 +910,10 @@ def _strip_content_type(headers: dict[str, str]) -> dict[str, str]: | ||||
|     return {k: v for k, v in headers.items() if k.lower() != "content-type"} | ||||
|  | ||||
|  | ||||
| def _add_cache_control(headers: dict[str, str]) -> dict[str, str]: | ||||
|     return {**headers, "Cache-Control": "max-age=31536000"} | ||||
|  | ||||
|  | ||||
| @app.get("/proxy/media/{encoded_url}") | ||||
| async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResponse: | ||||
|     # Decode the base64-encoded URL | ||||
| @@ -921,18 +925,20 @@ async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResp | ||||
|     return StreamingResponse( | ||||
|         proxy_resp.aiter_raw(), | ||||
|         status_code=proxy_resp.status_code, | ||||
|         headers=_filter_proxy_resp_headers( | ||||
|         headers=_add_cache_control( | ||||
|             _filter_proxy_resp_headers( | ||||
|                 proxy_resp, | ||||
|                 [ | ||||
|                     "content-length", | ||||
|                     "content-type", | ||||
|                     "content-range", | ||||
|                 "accept-ranges" "etag", | ||||
|                 "cache-control", | ||||
|                     "accept-ranges", | ||||
|                     "etag", | ||||
|                     "expires", | ||||
|                     "date", | ||||
|                     "last-modified", | ||||
|                 ], | ||||
|             ) | ||||
|         ), | ||||
|         background=BackgroundTask(proxy_resp.aclose), | ||||
|     ) | ||||
| @@ -967,16 +973,17 @@ async def serve_proxy_media_resized( | ||||
|         ) | ||||
|  | ||||
|     # Filter the headers | ||||
|     proxy_resp_headers = _filter_proxy_resp_headers( | ||||
|     proxy_resp_headers = _add_cache_control( | ||||
|         _filter_proxy_resp_headers( | ||||
|             proxy_resp, | ||||
|             [ | ||||
|                 "content-type", | ||||
|                 "etag", | ||||
|             "cache-control", | ||||
|                 "expires", | ||||
|                 "last-modified", | ||||
|             ], | ||||
|         ) | ||||
|     ) | ||||
|  | ||||
|     try: | ||||
|         out = BytesIO(proxy_resp.content) | ||||
| @@ -1064,6 +1071,7 @@ async def serve_attachment_thumbnail( | ||||
|     return FileResponse( | ||||
|         UPLOAD_DIR / (content_hash + "_resized"), | ||||
|         media_type="image/webp", | ||||
|         headers={"Cache-Control": "max-age=31536000"}, | ||||
|     ) | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user