From 69f1b3168ac3f29fc4bafba0418769248b10062a Mon Sep 17 00:00:00 2001
From: Andrew Godwin
Date: Thu, 15 Dec 2022 00:35:04 -0700
Subject: Improve cacheability
---
core/middleware.py | 6 ++++--
core/models/config.py | 8 +++++---
core/uris.py | 4 ++--
mediaproxy/views.py | 1 +
takahe/settings.py | 6 +++++-
5 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/core/middleware.py b/core/middleware.py
index de35c4c..c1d92ef 100644
--- a/core/middleware.py
+++ b/core/middleware.py
@@ -6,9 +6,9 @@ from core import sentry
from core.models import Config
-class AcceptMiddleware:
+class HeadersMiddleware:
"""
- Detects any Accept headers signifying a fellow AP server is trying to get JSON.
+ Deals with Accept request headers, and Cache-Control response ones.
"""
def __init__(self, get_response):
@@ -22,6 +22,8 @@ class AcceptMiddleware:
or "application/activity" in accept
)
response = self.get_response(request)
+ if "Cache-Control" not in response.headers:
+ response.headers["Cache-Control"] = "private, max-age=0"
return response
diff --git a/core/models/config.py b/core/models/config.py
index 2bb0d75..8f5dc31 100644
--- a/core/models/config.py
+++ b/core/models/config.py
@@ -5,10 +5,10 @@ import pydantic
from asgiref.sync import sync_to_async
from django.core.files import File
from django.db import models
-from django.templatetags.static import static
from django.utils.functional import lazy
from core.uploads import upload_namer
+from core.uris import StaticAbsoluteUrl
from takahe import __version__
@@ -201,8 +201,10 @@ class Config(models.Model):
site_name: str = "Takahē"
highlight_color: str = "#449c8c"
site_about: str = "
Welcome!
\n\nThis is a community running Takahē."
- site_icon: UploadedImage = static("img/icon-128.png")
- site_banner: UploadedImage = static("img/fjords-banner-600.jpg")
+ site_icon: UploadedImage = StaticAbsoluteUrl("img/icon-128.png").relative # type: ignore
+ site_banner: UploadedImage = StaticAbsoluteUrl(
+ "img/fjords-banner-600.jpg"
+ ).relative # type: ignore
policy_terms: str = ""
policy_privacy: str = ""
diff --git a/core/uris.py b/core/uris.py
index 2800fd4..81f53b6 100644
--- a/core/uris.py
+++ b/core/uris.py
@@ -1,7 +1,7 @@
from urllib.parse import urljoin
from django.conf import settings
-from django.templatetags.static import static
+from django.contrib.staticfiles.storage import staticfiles_storage
class RelativeAbsoluteUrl:
@@ -41,7 +41,7 @@ class StaticAbsoluteUrl(RelativeAbsoluteUrl):
"""
def __init__(self, path: str):
- static_url = static(path)
+ static_url = staticfiles_storage.url(path)
if "://" in static_url:
super().__init__(static_url)
else:
diff --git a/mediaproxy/views.py b/mediaproxy/views.py
index a44abb6..57257f3 100644
--- a/mediaproxy/views.py
+++ b/mediaproxy/views.py
@@ -49,6 +49,7 @@ class BaseCacheView(View):
cached_content["content"],
headers={
"Content-Type": cached_content["mimetype"],
+ "Cache-Control": "public, max-age=3600",
},
)
diff --git a/takahe/settings.py b/takahe/settings.py
index 4e09a10..91bfe7b 100644
--- a/takahe/settings.py
+++ b/takahe/settings.py
@@ -190,7 +190,7 @@ MIDDLEWARE = [
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware",
- "core.middleware.AcceptMiddleware",
+ "core.middleware.HeadersMiddleware",
"core.middleware.ConfigLoadingMiddleware",
"api.middleware.ApiTokenMiddleware",
"users.middleware.IdentityMiddleware",
@@ -274,6 +274,10 @@ STATICFILES_FINDERS = [
STATICFILES_DIRS = [BASE_DIR / "static"]
+STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
+
+WHITENOISE_MAX_AGE = 3600
+
STATIC_ROOT = BASE_DIR / "static-collected"
ALLOWED_HOSTS = SETUP.ALLOWED_HOSTS
--
cgit v1.2.3