Skip to content

Commit

Permalink
fix: Fix alternate URL on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed May 24, 2024
1 parent 7456ca6 commit cc54001
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions data_registry/templatetags/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ def catalog_str():


@register.simple_tag(takes_context=True)
def canonical_url(context):
def canonical_url(context, language=None):
request = context["request"]
name = urls.resolve(request.path).url_name

if name == "index" and get_language() == "en":
# / is the canonical of /en/.
args = ["/"]
if name == "search":
# Avoid duplicate content across different filters. Okay since there's no pagination.
args = [urls.reverse("search")]
else:
args = []
if language is None:
language = get_language()

match (name, language):
case ("index", "en"):
# / is the canonical of /en/.
args = ["/"]
case ("search", _):
# Avoid duplicate content across different filters. Okay since there's no pagination.
args = [urls.reverse("search")]
case _:
args = []
return request.build_absolute_uri(*args)


Expand All @@ -67,7 +71,7 @@ def translate_url(context, language):
request = context["request"]
name = urls.resolve(request.path).url_name

url = canonical_url(context)
url = canonical_url(context, language)
if name == "index" and language == "en":
# Translating "/es/" to "en" would return "/en/", which is not canonical.
return url
Expand Down

0 comments on commit cc54001

Please sign in to comment.