Current page tweaks

* Add link rel canonical to all pages (/ points itself instead of /?page=current-season because Google crawls duplicate versions of pages less often. I want / to be crawled frequently, so it has to be the canonical version of the content it holds)
* Fix a bug where passing an invald page slug would mean that no page was highlighted as current in the footer
This commit is contained in:
Matthias Portzel 2022-05-12 08:16:44 -05:00
parent 7f7d21384e
commit 40fd0aaa5c
3 changed files with 21 additions and 12 deletions

View File

@ -6,7 +6,7 @@ import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'whispermaphone.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "whispermaphone.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:

View File

@ -12,6 +12,11 @@
{% block head %}
<link rel="alternate" href="/feed" type="application/rss+xml" title="RSS">
{% if not first_page %}
<link rel="canonical" href="/?page={{ current_page_slug }}">
{% else %}
<link rel="canonical" href="/">
{% endif %}
<link href="{% static 'thoughts/codehighlight.css' %}" rel="stylesheet">
{% endblock %}
@ -21,7 +26,7 @@
<nav class="history-nav top" aria-label="History Navigation">
<ul>
{% for page in pages %}
{% if page.slug == current_page %}
{% if page.slug == current_page_slug %}
<li><span class="current-page">{{ page.formatted_name }}</span></li>
{% else %}
<li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li>
@ -91,7 +96,7 @@
<nav class="history-nav bottom" aria-label="History Navigation">
<ul>
{% for page in pages %}
{% if page.slug == current_page %}
{% if page.slug == current_page_slug %}
<li><span class="current-page">{{ page.formatted_name }}</span></li>
{% else %}
<li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li>

View File

@ -35,12 +35,10 @@ def index(request):
except ValueError:
highlighted_uuid = ""
# Figure out what page we're viewing
## Figure out what page we're viewing
pages = get_all_pages()
# First item in pages should be listed first
requested_page = pages[0]
requested_slug = request.GET.get("page", default=requested_page.slug)
requested_slug = request.GET.get("page", default="")
# show=uuid takes priority over page
if highlighted_uuid:
@ -50,7 +48,13 @@ def index(request):
except Thought.DoesNotExist:
pass
if requested_page.slug != requested_slug:
# When we get here, either:
# no slug was passed, requested_slug is ""
# a valid highlighted_uuid was passed, requested_slug is the slug of that page
# requested_slug is an invalid slug
# requested_slug is a valid slug
# First item in pages should be listed first
requested_page = pages[0]
for p in pages:
if p.slug == requested_slug:
requested_page = p
@ -62,7 +66,7 @@ def index(request):
"highlighted": highlighted_uuid,
"authenticated": authenticated,
"pages": pages,
"current_page": requested_slug,
"current_page_slug": requested_page.slug,
"first_page": requested_page == pages[0] # if you're viewing the first page
})