Use wagtail's new built-in cache tags
They're good, because I wrote them
This commit is contained in:
parent
48e36bc5b9
commit
518461a88f
6 changed files with 93 additions and 103 deletions
|
@ -1,12 +1,12 @@
|
||||||
{% extends "common/content_page.html" %}
|
{% extends "common/content_page.html" %}
|
||||||
|
|
||||||
{% load cache util_tags %}
|
{% load wagtail_cache %}
|
||||||
|
|
||||||
{% block post_content %}
|
{% block post_content %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
|
|
||||||
{% if not request.is_preview %}
|
{% if not request.is_preview %}
|
||||||
{% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "similar-content" page.id request.is_preview %}
|
{% wagtailpagecache FRAGMENT_CACHE_TTL "similar-content" %}
|
||||||
<section class="container similar-content" id="similar-content">
|
<section class="container similar-content" id="similar-content">
|
||||||
<h2 class="subtitle is-size-2">Similar content</h2>
|
<h2 class="subtitle is-size-2">Similar content</h2>
|
||||||
|
|
||||||
|
@ -21,6 +21,6 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
{% endcache %}
|
{% endwagtailpagecache %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -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>
|
<!DOCTYPE html>
|
||||||
<html lang="en-GB">
|
<html lang="en-GB">
|
||||||
|
@ -32,9 +32,9 @@
|
||||||
<body class="{% block body_class %}{% endblock %}">
|
<body class="{% block body_class %}{% endblock %}">
|
||||||
{% wagtailuserbar %}
|
{% wagtailuserbar %}
|
||||||
|
|
||||||
{% cache 1800 "navbar" request.is_preview %}
|
{% wagtailcache 1800 "navbar" %}
|
||||||
{% navbar %}
|
{% navbar %}
|
||||||
{% endcache %}
|
{% endwagtailcache %}
|
||||||
|
|
||||||
{% block main %}
|
{% block main %}
|
||||||
<main>
|
<main>
|
||||||
|
@ -42,21 +42,21 @@
|
||||||
</main>
|
</main>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% cache 1800 "footer" request.is_preview %}
|
{% wagtailcache 1800 "footer" %}
|
||||||
{% footer %}
|
{% footer %}
|
||||||
{% endcache %}
|
{% endwagtailcache %}
|
||||||
|
|
||||||
{# Not async to avoid bright flashes #}
|
{# Not async to avoid bright flashes #}
|
||||||
{% sri_static "js/dark-mode.js" %}
|
{% 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 %}
|
{% block plausible %}
|
||||||
{% if not request.user.is_authenticated or not request.is_preview %}
|
{% if not request.user.is_authenticated or not request.is_preview %}
|
||||||
{% plausible %}
|
{% plausible %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{% 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 %}
|
{% wagtailpagecache FRAGMENT_CACHE_TTL "content-details" %}
|
||||||
<div class="content-details field is-grouped">
|
<div class="content-details field is-grouped">
|
||||||
{% if page.date %}
|
{% if page.date %}
|
||||||
<span class="icon-text">
|
<span class="icon-text">
|
||||||
<span class="icon">
|
<span class="icon">
|
||||||
|
@ -32,5 +32,5 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endcache %}
|
{% endwagtailpagecache %}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{% 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 %}
|
{% wagtailpagecache FRAGMENT_CACHE_TTL "listing-item" breadcrumbs %}
|
||||||
<article class="media listing-item">
|
<article class="media listing-item">
|
||||||
<div class="columns">
|
<div class="columns">
|
||||||
<figure class="media-left column is-3 image-column">
|
<figure class="media-left column is-3 image-column">
|
||||||
{% if page.list_image_url %}
|
{% if page.list_image_url %}
|
||||||
|
@ -23,5 +23,5 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
{% endcache %}
|
{% endwagtailpagecache %}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import random
|
|
||||||
|
|
||||||
from django.template import Library
|
from django.template import Library
|
||||||
from django.utils.encoding import force_str
|
from django.utils.encoding import force_str
|
||||||
from wagtail.models import Page
|
from wagtail.models import Page
|
||||||
|
@ -20,11 +18,6 @@ def pagefullurl(context: dict, page: Page) -> str:
|
||||||
return page.get_full_url(context["request"])
|
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()
|
@register.filter()
|
||||||
def extract_text(html: str | RichText) -> str:
|
def extract_text(html: str | RichText) -> str:
|
||||||
return utils.extract_text(force_str(html))
|
return utils.extract_text(force_str(html))
|
||||||
|
|
|
@ -3,12 +3,9 @@ from django.http.request import HttpRequest
|
||||||
|
|
||||||
|
|
||||||
def global_vars(request: HttpRequest) -> dict:
|
def global_vars(request: HttpRequest) -> dict:
|
||||||
# noop caching in preview
|
|
||||||
fragment_cache_ttl = 0 if getattr(request, "is_preview", False) else 3600
|
|
||||||
return {
|
return {
|
||||||
"SEO_INDEX": settings.SEO_INDEX,
|
"SEO_INDEX": settings.SEO_INDEX,
|
||||||
"DEBUG": settings.DEBUG,
|
"DEBUG": settings.DEBUG,
|
||||||
"FRAGMENT_CACHE_TTL": fragment_cache_ttl,
|
"FRAGMENT_CACHE_TTL": 3600,
|
||||||
"FRAGMENT_CACHE_TTL_JITTER": fragment_cache_ttl * 0.1,
|
|
||||||
"ACTIVITYPUB_HOST": settings.ACTIVITYPUB_HOST,
|
"ACTIVITYPUB_HOST": settings.ACTIVITYPUB_HOST,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue