Generate a robots.txt (#1068)#1080
Open
dduugg wants to merge 2 commits into
Open
Conversation
Hugo skips robots.txt unless `enableRobotsTXT` is set, so https://www.shift2bikes.org/robots.txt has been a 404. Turn it on, and supply a template rather than take hugo's permissive default. Search indexing stays open for all real content, event images, and the calendar; crawling is turned away from the emailed event-management links and from the two netlify rules that proxy through to our own api server.
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.
Fixes #1068.
https://www.shift2bikes.org/robots.txtreturns a 404. Hugo only emits the file whenenableRobotsTXTis set, and it never has been. There is nosite/static/robots.txtor template filling the gap either.Changes
site/config/_default/hugo.toml: setenableRobotsTXT = true.site/layouts/robots.txt: new template, so we ship a considered policy rather than hugo's permissive built-in default (which is justUser-agent: *).The issue asks to "allow search indexing but disallow any nefarious behaviour." The template keeps indexing open for all real content, event images, event detail pages, and the calendar, and turns crawlers away from three things:
Disallow: /addevent/edit-netlify.tomlrewrites/addevent/*to the add-event page with a 200, so the emailed management links (/addevent/edit-<series>-<secret>) are real, fetchable URLs. They are unlisted, but should never be indexed if one leaks. robots.txt matching is prefix-based, so this fences off the secret links while leaving the public/addevent/page indexable.Disallow: /api/and/socialapi/Disallow: /404.htmlVerification
Built with the same command netlify runs for production (
npm run deploycallshugo -s site) and confirmed the file lands atsite/public/robots.txt, which is thepublishdirectory declared innetlify.toml. None of the redirect rules innetlify.tomlshadow/robots.txt, and netlify serves real files ahead of redirects regardless.The design rests on prefix matching, so the built file was checked against a real parser (Python's
urllib.robotparser) rather than by inspection:Worth noting that
enableRobotsTXTdoes take effect even though[outputs] homeis explicitly overridden inhugo.toml. Hugo treatsrobotstxtas its own page kind, not as a home output format, so the two do not interact. Confirmed against the pinned hugo 0.152.2.No backend or frontend code is touched, so there is nothing for the
apptest suite to cover here.Dependency on #1083
The 404 rule is written as
Disallow: /404.htmlto match #1083 (clean up 404 page), which moves the page from hugo's default/404/index.htmlto/404.html. Onmaintoday the page is also reachable at/404.html(netlify serves it there for unmatched URLs, and apostdeploycopy places it there), so this rule is already correct against production and stays correct once #1083 lands. Whichever of the two PRs merges second, no further change to this rule is needed.Known limitation: the sitemap is not yet valid
This PR stops the 404. It does not make the advertised sitemap work, and reviewers should not assume otherwise.
Because
baseURL = "/", every entry in the production sitemap is a relative path:The sitemap protocol requires absolute URLs, so search engines will reject these. The
Sitemap:line is included anyway: it costs nothing, it is correct the momentbaseURLis fixed, and the template carries an inline comment saying as much. Happy to drop the line if reviewers would rather not advertise the sitemap until it is valid.The fix is not a one-line
baseURLchange. The theme leans onabsURLthroughout, including for the calendar's own API calls:Setting
baseURLto the production URL would make deploy previews and local dev load their assets from, and fetch their event data from,www.shift2bikes.org. Doing it properly means a per-contextHUGO_BASEURLinnetlify.toml(production gets the canonical URL, previews get$DEPLOY_PRIME_URL). That is its own change with its own blast radius, and is better tracked separately. The production sitemap also lists/404/, which this PR does not address.