forked from forks/microblog.pub
Improve static assets caching
This commit is contained in:
parent
372851caaf
commit
88cb82c9bb
1 changed files with 30 additions and 22 deletions
20
app/main.py
20
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(
|
||||||
|
_filter_proxy_resp_headers(
|
||||||
proxy_resp,
|
proxy_resp,
|
||||||
[
|
[
|
||||||
"content-length",
|
"content-length",
|
||||||
"content-type",
|
"content-type",
|
||||||
"content-range",
|
"content-range",
|
||||||
"accept-ranges" "etag",
|
"accept-ranges",
|
||||||
"cache-control",
|
"etag",
|
||||||
"expires",
|
"expires",
|
||||||
"date",
|
"date",
|
||||||
"last-modified",
|
"last-modified",
|
||||||
],
|
],
|
||||||
|
)
|
||||||
),
|
),
|
||||||
background=BackgroundTask(proxy_resp.aclose),
|
background=BackgroundTask(proxy_resp.aclose),
|
||||||
)
|
)
|
||||||
|
@ -967,16 +973,17 @@ 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(
|
||||||
|
_filter_proxy_resp_headers(
|
||||||
proxy_resp,
|
proxy_resp,
|
||||||
[
|
[
|
||||||
"content-type",
|
"content-type",
|
||||||
"etag",
|
"etag",
|
||||||
"cache-control",
|
|
||||||
"expires",
|
"expires",
|
||||||
"last-modified",
|
"last-modified",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
out = BytesIO(proxy_resp.content)
|
out = BytesIO(proxy_resp.content)
|
||||||
|
@ -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