Skip to content

Commit

Permalink
Merge pull request #1715 from nationalarchives/fix/incorrect-noindex
Browse files Browse the repository at this point in the history
Fix for some incorrect noindex behaviours
  • Loading branch information
jacksonj04 authored Jan 6, 2025
2 parents d7ea425 + a8f26f6 commit f154976
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.parse import urlencode

from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import reverse

Expand All @@ -13,9 +14,16 @@ def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
response = self.get_response(request)

# If the response is a redirect, short-circuit adding the X-Robots-Tag
if isinstance(response, HttpResponseRedirect):
return response

# If page_allow_index is True, short-circuit adding the X-Robots-Tag
if isinstance(response, TemplateResponse) and response.context_data.get("page_allow_index", False):
return response

# In all other cases, assume we don't want it indexing and add the noindex X-Robots-Tag.
response.headers["X-Robots-Tag"] = "noindex,nofollow"
return response

Expand Down
1 change: 1 addition & 0 deletions config/views/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CourtsTribunalsListView(TemplateViewWithContext):

template_name = "pages/courts_and_tribunals.html"
page_title = "Judgments and decisions by court or tribunal"
page_allow_index = True

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand Down
7 changes: 7 additions & 0 deletions judgments/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ def test_static_pages(self):
response = self.client.get(f"/{url}", follow=True)
assert response.status_code == 200

def test_redirect_has_no_noindex(self):
"""Make sure that redirection responses can be indexed."""

response = self.client.get("/what-to-expect")
assert response.status_code == 302
assert response.headers.get("X-Robots-Tag") is None


class TestBackLink(TestCase):
def test_referrer_is_search_page_without_query(self):
Expand Down

0 comments on commit f154976

Please sign in to comment.