From 518461a88f3e59c045b54e288240c69a69524cca Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 5 Jan 2024 15:44:45 +0000 Subject: [PATCH] Use wagtail's new built-in cache tags They're good, because I wrote them --- .../blog/templates/blog/blog_post_page.html | 30 ++++----- website/common/templates/base.html | 44 ++++++------- .../templates/common/content-details.html | 64 +++++++++---------- .../common/templates/common/listing-item.html | 46 ++++++------- website/common/templatetags/util_tags.py | 7 -- website/utils/context_processors.py | 5 +- 6 files changed, 93 insertions(+), 103 deletions(-) diff --git a/website/blog/templates/blog/blog_post_page.html b/website/blog/templates/blog/blog_post_page.html index 7c4928d..f6f2a2b 100644 --- a/website/blog/templates/blog/blog_post_page.html +++ b/website/blog/templates/blog/blog_post_page.html @@ -1,26 +1,26 @@ {% extends "common/content_page.html" %} -{% load cache util_tags %} +{% load wagtail_cache %} {% block post_content %} {{ block.super }} {% if not request.is_preview %} - {% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "similar-content" page.id request.is_preview %} -
-

Similar content

+ {% wagtailpagecache FRAGMENT_CACHE_TTL "similar-content" %} +
+

Similar content

-

- View all → -

+

+ View all → +

- {% for page in page.get_similar_posts %} - {% block listing_item %} - {% include "common/listing-item.html" %} - {% endblock %} - {% endfor %} + {% for page in page.get_similar_posts %} + {% block listing_item %} + {% include "common/listing-item.html" %} + {% endblock %} + {% endfor %} -
- {% endcache %} - {% endif %} +
+ {% endwagtailpagecache %} +{% endif %} {% endblock %} diff --git a/website/common/templates/base.html b/website/common/templates/base.html index 90c9fc2..e3522c5 100644 --- a/website/common/templates/base.html +++ b/website/common/templates/base.html @@ -1,4 +1,4 @@ -{% load static wagtailcore_tags wagtailuserbar navbar_tags footer_tags plausible_wagtail favicon_tags sri cache %} +{% load static wagtailcore_tags wagtailuserbar navbar_tags footer_tags plausible_wagtail favicon_tags sri wagtail_cache %} @@ -32,31 +32,31 @@ {% wagtailuserbar %} - {% cache 1800 "navbar" request.is_preview %} - {% navbar %} - {% endcache %} + {% wagtailcache 1800 "navbar" %} + {% navbar %} + {% endwagtailcache %} - {% block main %} -
- {% block main_content %}{% endblock %} -
- {% endblock %} + {% block main %} +
+ {% block main_content %}{% endblock %} +
+ {% endblock %} - {% cache 1800 "footer" request.is_preview %} - {% footer %} - {% endcache %} + {% wagtailcache 1800 "footer" %} + {% footer %} +{% endwagtailcache %} - {# Not async to avoid bright flashes #} - {% sri_static "js/dark-mode.js" %} +{# Not async to avoid bright flashes #} +{% sri_static "js/dark-mode.js" %} - + - {% block extra_js %}{% endblock %} +{% block extra_js %}{% endblock %} - {% block plausible %} - {% if not request.user.is_authenticated or not request.is_preview %} - {% plausible %} - {% endif %} - {% endblock %} - +{% block plausible %} + {% if not request.user.is_authenticated or not request.is_preview %} + {% plausible %} + {% endif %} +{% endblock %} + diff --git a/website/common/templates/common/content-details.html b/website/common/templates/common/content-details.html index 109c179..e99f06b 100644 --- a/website/common/templates/common/content-details.html +++ b/website/common/templates/common/content-details.html @@ -1,36 +1,36 @@ -{% load wagtailcore_tags cache util_tags %} +{% load wagtailcore_tags wagtail_cache %} -{% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "content-details" page.id request.is_preview %} -
- {% if page.date %} - - - - - {{ page.date|date:"Y-m-d" }} +{% wagtailpagecache FRAGMENT_CACHE_TTL "content-details" %} +
+ {% if page.date %} + + + - {% endif %} + {{ page.date|date:"Y-m-d" }} + + {% endif %} - {% if page.show_reading_time %} -
- - - - {{ page.reading_time_display }} -
- {% endif %} + {% if page.show_reading_time %} +
+ + + + {{ page.reading_time_display }} +
+ {% endif %} - {% if page.tags.all %} -
- - - - - - {% for tag in page.tags.all|dictsort:"slug" %} - #{{ tag.slug }} - {% endfor %} -
- {% endif %} -
-{% endcache %} + {% if page.tags.all %} +
+ + + + + + {% for tag in page.tags.all|dictsort:"slug" %} + #{{ tag.slug }} + {% endfor %} +
+ {% endif %} +
+{% endwagtailpagecache %} diff --git a/website/common/templates/common/listing-item.html b/website/common/templates/common/listing-item.html index 5d8eeb8..0321cb9 100644 --- a/website/common/templates/common/listing-item.html +++ b/website/common/templates/common/listing-item.html @@ -1,27 +1,27 @@ -{% load wagtailcore_tags cache util_tags %} +{% load wagtailcore_tags wagtail_cache util_tags %} -{% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "listing-item" page.id request.is_preview breadcrumbs %} -
-
-
- {% if page.list_image_url %} - - - +{% wagtailpagecache FRAGMENT_CACHE_TTL "listing-item" breadcrumbs %} +
+
+
+ {% if page.list_image_url %} + + + + {% endif %} +
+
+
+ {% if breadcrumbs %} + {% include "common/breadcrumbs.html" with parents=page.get_parent_pages %} {% endif %} -
-
-
- {% if breadcrumbs %} - {% include "common/breadcrumbs.html" with parents=page.get_parent_pages %} - {% endif %} -

- {{ page.title }} -

- {% include "common/content-details.html" %} -

{{ page.summary }}

-
+

+ {{ page.title }} +

+ {% include "common/content-details.html" %} +

{{ page.summary }}

-
-{% endcache %} + + +{% endwagtailpagecache %} diff --git a/website/common/templatetags/util_tags.py b/website/common/templatetags/util_tags.py index 06e4b82..bc7e1cd 100644 --- a/website/common/templatetags/util_tags.py +++ b/website/common/templatetags/util_tags.py @@ -1,5 +1,3 @@ -import random - from django.template import Library from django.utils.encoding import force_str from wagtail.models import Page @@ -20,11 +18,6 @@ def pagefullurl(context: dict, page: Page) -> str: return page.get_full_url(context["request"]) -@register.filter() -def jitter(original: float, jitter: float) -> float: - return random.uniform(original + jitter, original - jitter) - - @register.filter() def extract_text(html: str | RichText) -> str: return utils.extract_text(force_str(html)) diff --git a/website/utils/context_processors.py b/website/utils/context_processors.py index 1406632..44a4117 100644 --- a/website/utils/context_processors.py +++ b/website/utils/context_processors.py @@ -3,12 +3,9 @@ from django.http.request import HttpRequest def global_vars(request: HttpRequest) -> dict: - # noop caching in preview - fragment_cache_ttl = 0 if getattr(request, "is_preview", False) else 3600 return { "SEO_INDEX": settings.SEO_INDEX, "DEBUG": settings.DEBUG, - "FRAGMENT_CACHE_TTL": fragment_cache_ttl, - "FRAGMENT_CACHE_TTL_JITTER": fragment_cache_ttl * 0.1, + "FRAGMENT_CACHE_TTL": 3600, "ACTIVITYPUB_HOST": settings.ACTIVITYPUB_HOST, }