diff --git a/website/common/tests/test_views.py b/website/common/tests/test_views.py index ac3deaf..edc2cfb 100644 --- a/website/common/tests/test_views.py +++ b/website/common/tests/test_views.py @@ -16,7 +16,7 @@ class Error404PageTestCase(TestCase): ) def test_actual_404_no_url_match(self) -> None: - response = self.client.get("/favicon.ico") + response = self.client.get("/missing.file") self.assertContains( response, "

There's nothing here!

", html=True, status_code=404 ) diff --git a/website/common/views.py b/website/common/views.py index 31bae65..108a663 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -9,9 +9,11 @@ from django.urls import reverse from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_control, cache_page from django.views.defaults import ERROR_404_TEMPLATE_NAME -from django.views.generic import TemplateView +from django.views.generic import RedirectView, TemplateView from wagtail.models import Page from wagtail.query import PageQuerySet +from wagtail_favicon.models import FaviconSettings +from wagtail_favicon.utils import get_rendition_url from website.common.utils import get_site_title from website.contrib.singleton_page.utils import SingletonPageCache @@ -139,3 +141,15 @@ class ContentPageFeed(AllPagesFeed): def items(self) -> PageQuerySet: return self.posts + + +@method_decorator(cache_control(max_age=60 * 60), name="dispatch") +class FaviconView(RedirectView): + def get_redirect_url(self) -> str: + favicon_settings = FaviconSettings.for_request(self.request) + size = FaviconSettings.icon_sizes[0] + + # Force image to PNG + return get_rendition_url( + favicon_settings.base_favicon_image, f"fill-{size}|format-png" + ) diff --git a/website/urls.py b/website/urls.py index 8913643..63abd37 100644 --- a/website/urls.py +++ b/website/urls.py @@ -14,7 +14,13 @@ from wagtail.images.views.serve import ServeView from wagtail_favicon.urls import urls as favicon_urls from wagtailautocomplete.urls.admin import urlpatterns as autocomplete_admin_urls -from website.common.views import AllPagesFeed, KeybaseView, RobotsView, page_not_found +from website.common.views import ( + AllPagesFeed, + FaviconView, + KeybaseView, + RobotsView, + page_not_found, +) urlpatterns = [ path("admin/autocomplete/", include(autocomplete_admin_urls)), @@ -49,6 +55,7 @@ urlpatterns = [ "@jake", RedirectView.as_view(url=f"https://{settings.ACTIVITYPUB_HOST}/@jake"), ), + path("favicon.ico", FaviconView.as_view()), path("", include(favicon_urls)), ]