Add dynamic sitemap.xml and robots.txt (#1252) - #1307
Merged
Conversation
Generates /sitemap.xml from our querysets via django.contrib.sitemaps so the sitemap stays current with zero maintenance, and serves an environment-aware /robots.txt from a Django view (no web-server access needed). - settings: add 'django.contrib.sitemaps' to INSTALLED_APPS. Deliberately NOT installing django.contrib.sites — without it the framework falls back to RequestSite, deriving the domain from the request host, so the sitemap emits the correct domain across local/test/prod with no per-env config and no extra DB migration. - website/sitemaps.py: StaticViewSitemap (home/people/publications/projects/ awards/news), ProjectSitemap (is_visible only), PersonSitemap (people with a Position, excluding the placeholder url_name), NewsSitemap. lastmod from existing date fields; explicit ordering for stable pagination. Publications have no detail page, so they're covered by the static listing entry. - website/views/robots.py: PROD allows crawling and advertises the sitemap; every other environment (notably the TEST server, DJANGO_ENV=TEST) returns Disallow: / so the test site isn't indexed. - urls: wire sitemap.xml and robots.txt above the website.urls catch-all. - tests: 9 regression tests covering both endpoints. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1252.
What this does
Adds a dynamic XML sitemap and an environment-aware robots.txt, both served by Django (no web-server access required).
The sitemap is generated from our querysets via
django.contrib.sitemaps, so it's always current — no static file to regenerate when content changes. See the issue discussion for why this beats the crawler/plugin/manual-file approaches.Changes
settings.py— add'django.contrib.sitemaps'toINSTALLED_APPS. Deliberately not installingdjango.contrib.sites: without it the framework falls back toRequestSite, deriving the domain from the request host, so the sitemap emits the correct domain across local/test/prod with no per-env config and no extra DB migration.website/sitemaps.py— four sitemaps:StaticViewSitemap—/,/people/,/publications/,/projects/,/awards/,/news/ProjectSitemap— visible projects only (is_visible=True),lastmodfromupdatedPersonSitemap— people with ≥1Position, excluding theplaceholderurl_name,lastmodfrombio_datetime_modifiedNewsSitemap— news items,lastmodfromdate/publications/entry.website/views/robots.py—robots.txt: PROD allows crawling and advertises the sitemap; every other environment (notably the TEST server,DJANGO_ENV=TEST) returnsDisallow: /so the test site isn't indexed and can't compete with prod in search results.makeabilitylab/urls.py— routesitemap.xmlandrobots.txtabove thewebsite.urlscatch-all so it can't shadow them.website/tests/test_sitemap.py— 9 regression tests (XML content-type, static pages present, visible-vs-private project, person with/without position, news item, robots PROD vs non-PROD).Sample output (local dev server)
GET /sitemap.xml(excerpt):GET /robots.txt(dev/test — non-PROD):GET /robots.txt(PROD):Testing
website/tests/test_sitemap.py— 9 tests, all pass.Ran 168 tests ... OK.curlagainst the local dev server.Note (no UI change)
This is a non-visual change, so no before/after screenshots — sample output above stands in. After this merges and deploys to prod, the sitemap still needs to be submitted once in Google Search Console (a manual, account-side step).
🤖 Generated with Claude Code