Use wagtail's new built-in cache tags

They're good, because I wrote them
This commit is contained in:
Jake Howard 2024-01-05 15:44:45 +00:00
parent 48e36bc5b9
commit 518461a88f
Signed by: jake
GPG key ID: 57AFB45680EDD477
6 changed files with 93 additions and 103 deletions

View file

@ -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 %}
<section class="container similar-content" id="similar-content">
<h2 class="subtitle is-size-2">Similar content</h2>
{% wagtailpagecache FRAGMENT_CACHE_TTL "similar-content" %}
<section class="container similar-content" id="similar-content">
<h2 class="subtitle is-size-2">Similar content</h2>
<p class="view-all">
<a href="{{ page.blog_post_list_page_url }}">View all &rarr;</a>
</p>
<p class="view-all">
<a href="{{ page.blog_post_list_page_url }}">View all &rarr;</a>
</p>
{% 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 %}
</section>
{% endcache %}
{% endif %}
</section>
{% endwagtailpagecache %}
{% endif %}
{% endblock %}

View file

@ -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 %}
<!DOCTYPE html>
<html lang="en-GB">
@ -32,31 +32,31 @@
<body class="{% block body_class %}{% endblock %}">
{% wagtailuserbar %}
{% cache 1800 "navbar" request.is_preview %}
{% navbar %}
{% endcache %}
{% wagtailcache 1800 "navbar" %}
{% navbar %}
{% endwagtailcache %}
{% block main %}
<main>
{% block main_content %}{% endblock %}
</main>
{% endblock %}
{% block main %}
<main>
{% block main_content %}{% endblock %}
</main>
{% 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" %}
<script async defer type="text/javascript" src="{% static 'js/base.js' %}" integrity="{% sri_integrity_static 'js/base.js' %}"></script>
<script async defer type="text/javascript" src="{% static 'js/base.js' %}" integrity="{% sri_integrity_static 'js/base.js' %}"></script>
{% block extra_js %}{% endblock %}
{% block extra_js %}{% endblock %}
{% block plausible %}
{% if not request.user.is_authenticated or not request.is_preview %}
{% plausible %}
{% endif %}
{% endblock %}
</body>
{% block plausible %}
{% if not request.user.is_authenticated or not request.is_preview %}
{% plausible %}
{% endif %}
{% endblock %}
</body>
</html>

View file

@ -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 %}
<div class="content-details field is-grouped">
{% if page.date %}
<span class="icon-text">
<span class="icon">
<i class="far fa-lg fa-calendar-alt"></i>
</span>
<span>{{ page.date|date:"Y-m-d" }}</span>
{% wagtailpagecache FRAGMENT_CACHE_TTL "content-details" %}
<div class="content-details field is-grouped">
{% if page.date %}
<span class="icon-text">
<span class="icon">
<i class="far fa-lg fa-calendar-alt"></i>
</span>
{% endif %}
<span>{{ page.date|date:"Y-m-d" }}</span>
</span>
{% endif %}
{% if page.show_reading_time %}
<div class="icon-text" {% if page.word_count %}title="{{ page.word_count }} words"{% endif %}>
<span class="icon">
<i class="far fa-lg fa-clock"></i>
</span>
<span>{{ page.reading_time_display }}</span>
</div>
{% endif %}
{% if page.show_reading_time %}
<div class="icon-text" {% if page.word_count %}title="{{ page.word_count }} words"{% endif %}>
<span class="icon">
<i class="far fa-lg fa-clock"></i>
</span>
<span>{{ page.reading_time_display }}</span>
</div>
{% endif %}
{% if page.tags.all %}
<div class="icon-text is-family-code">
<span class="icon">
<a href="{{ page.tag_list_page_url }}" title="View all tags">
<i class="fas fa-lg fa-tags"></i>
</a>
</span>
{% for tag in page.tags.all|dictsort:"slug" %}
<span><a title="{{ tag.name }}" href="{% pageurl tag %}">#{{ tag.slug }}</a></span>
{% endfor %}
</div>
{% endif %}
</div>
{% endcache %}
{% if page.tags.all %}
<div class="icon-text is-family-code">
<span class="icon">
<a href="{{ page.tag_list_page_url }}" title="View all tags">
<i class="fas fa-lg fa-tags"></i>
</a>
</span>
{% for tag in page.tags.all|dictsort:"slug" %}
<span><a title="{{ tag.name }}" href="{% pageurl tag %}">#{{ tag.slug }}</a></span>
{% endfor %}
</div>
{% endif %}
</div>
{% endwagtailpagecache %}

View file

@ -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 %}
<article class="media listing-item">
<div class="columns">
<figure class="media-left column is-3 image-column">
{% if page.list_image_url %}
<a href="{% pageurl page %}" class="image" title="{{ page.title }}">
<img src="{{ page.list_image_url }}" alt="" loading="lazy" decoding="async" />
</a>
{% wagtailpagecache FRAGMENT_CACHE_TTL "listing-item" breadcrumbs %}
<article class="media listing-item">
<div class="columns">
<figure class="media-left column is-3 image-column">
{% if page.list_image_url %}
<a href="{% pageurl page %}" class="image" title="{{ page.title }}">
<img src="{{ page.list_image_url }}" alt="" loading="lazy" decoding="async" />
</a>
{% endif %}
</figure>
<div class="media-content column">
<div>
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
</figure>
<div class="media-content column">
<div>
{% if breadcrumbs %}
{% include "common/breadcrumbs.html" with parents=page.get_parent_pages %}
{% endif %}
<h2 class="title is-3">
<a href="{% pageurl page %}">{{ page.title }}</a>
</h2>
{% include "common/content-details.html" %}
<p>{{ page.summary }}</p>
</div>
<h2 class="title is-3">
<a href="{% pageurl page %}">{{ page.title }}</a>
</h2>
{% include "common/content-details.html" %}
<p>{{ page.summary }}</p>
</div>
</div>
</article>
{% endcache %}
</div>
</article>
{% endwagtailpagecache %}

View file

@ -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))

View file

@ -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,
}