mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-12-22 05:04:27 +00:00
Improve static assets caching
This commit is contained in:
parent
372851caaf
commit
88cb82c9bb
1 changed files with 30 additions and 22 deletions
52
app/main.py
52
app/main.py
|
@ -119,7 +119,7 @@ class CustomMiddleware:
|
||||||
# And add the security headers
|
# And add the security headers
|
||||||
headers = MutableHeaders(scope=message)
|
headers = MutableHeaders(scope=message)
|
||||||
headers["X-Request-ID"] = request_id
|
headers["X-Request-ID"] = request_id
|
||||||
headers["Server"] = "microblogpub"
|
headers["x-powered-by"] = "microblogpub"
|
||||||
headers[
|
headers[
|
||||||
"referrer-policy"
|
"referrer-policy"
|
||||||
] = "no-referrer, strict-origin-when-cross-origin"
|
] = "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"}
|
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}")
|
@app.get("/proxy/media/{encoded_url}")
|
||||||
async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResponse:
|
async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResponse:
|
||||||
# Decode the base64-encoded URL
|
# Decode the base64-encoded URL
|
||||||
|
@ -921,18 +925,20 @@ async def serve_proxy_media(request: Request, encoded_url: str) -> StreamingResp
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
proxy_resp.aiter_raw(),
|
proxy_resp.aiter_raw(),
|
||||||
status_code=proxy_resp.status_code,
|
status_code=proxy_resp.status_code,
|
||||||
headers=_filter_proxy_resp_headers(
|
headers=_add_cache_control(
|
||||||
proxy_resp,
|
_filter_proxy_resp_headers(
|
||||||
[
|
proxy_resp,
|
||||||
"content-length",
|
[
|
||||||
"content-type",
|
"content-length",
|
||||||
"content-range",
|
"content-type",
|
||||||
"accept-ranges" "etag",
|
"content-range",
|
||||||
"cache-control",
|
"accept-ranges",
|
||||||
"expires",
|
"etag",
|
||||||
"date",
|
"expires",
|
||||||
"last-modified",
|
"date",
|
||||||
],
|
"last-modified",
|
||||||
|
],
|
||||||
|
)
|
||||||
),
|
),
|
||||||
background=BackgroundTask(proxy_resp.aclose),
|
background=BackgroundTask(proxy_resp.aclose),
|
||||||
)
|
)
|
||||||
|
@ -967,15 +973,16 @@ async def serve_proxy_media_resized(
|
||||||
)
|
)
|
||||||
|
|
||||||
# Filter the headers
|
# Filter the headers
|
||||||
proxy_resp_headers = _filter_proxy_resp_headers(
|
proxy_resp_headers = _add_cache_control(
|
||||||
proxy_resp,
|
_filter_proxy_resp_headers(
|
||||||
[
|
proxy_resp,
|
||||||
"content-type",
|
[
|
||||||
"etag",
|
"content-type",
|
||||||
"cache-control",
|
"etag",
|
||||||
"expires",
|
"expires",
|
||||||
"last-modified",
|
"last-modified",
|
||||||
],
|
],
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1064,6 +1071,7 @@ async def serve_attachment_thumbnail(
|
||||||
return FileResponse(
|
return FileResponse(
|
||||||
UPLOAD_DIR / (content_hash + "_resized"),
|
UPLOAD_DIR / (content_hash + "_resized"),
|
||||||
media_type="image/webp",
|
media_type="image/webp",
|
||||||
|
headers={"Cache-Control": "max-age=31536000"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue