Set an absolute baseURL per netlify context for the sitemap (#1082)#1084
Open
dduugg wants to merge 1 commit into
Open
Set an absolute baseURL per netlify context for the sitemap (#1082)#1084dduugg wants to merge 1 commit into
dduugg wants to merge 1 commit into
Conversation
…g#1082) The sitemap advertised at /sitemap.xml lists relative <loc> paths, because baseURL is "/". The sitemap protocol requires fully qualified urls, so search engines reject these entries. baseURL stays "/" in hugo.toml, since the theme resolves assets and the calendar's /api calls through absURL, and a relative base is what lets the same build work on localhost, deploy previews, and prod. Instead, override it per netlify context: production -> https://www.shift2bikes.org ( canonical host ) deploy/branch deploy -> $DEPLOY_PRIME_URL ( that build's own origin ) Set inline in `command`, since netlify only expands env vars like $DEPLOY_PRIME_URL there, not in a [context.*.environment] table. On previews the assets and /api proxy then resolve against the preview rather than production.
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 #1082.
https://www.shift2bikes.org/sitemap.xmlbuilds and serves (200), but every entry is a relative path:The sitemap protocol requires
<loc>to be a fully qualified URL, so these entries are almost certainly being discarded by search engines. Hugo builds<loc>from each page's.Permalink, which derives frombaseURL, andbaseURL = "/".Why not just change baseURL in hugo.toml
baseURL = "/"is load-bearing. The theme resolves assets and the calendar's own API calls throughabsURL:With a relative base these resolve same-origin, which is what lets one build work unchanged on localhost, on deploy previews, and in production. Hardcoding the production URL into
hugo.tomlwould make previews and local dev load assets from, and fetch event data from,www.shift2bikes.org.The change
baseURLstays/inhugo.toml.netlify.tomloverrides it per build context viaHUGO_BASEURL(hugo maps that env var ontobaseURL):https://www.shift2bikes.org(canonical host, matching the domain redirects at the top ofnetlify.toml)$DEPLOY_PRIME_URL(that build's own origin)/apiproxy resolve against the preview, not production/fromhugo.tomlIt is set inline in each
commandrather than in a[context.*.environment]table, because netlify only expands env vars like$DEPLOY_PRIME_URLinsidecommand. A[context.*.environment]value is a literal string, soHUGO_BASEURL = "$DEPLOY_PRIME_URL"there would hand hugo the literal text$DEPLOY_PRIME_URL.Verification
Built with hugo 0.152.2, reproducing each context's environment:
So the sitemap becomes absolute in the two deployed contexts, previews stay self-contained (the
/api/*proxy handles the preview-origin API calls), and local dev is unchanged.netlify.tomlwas confirmed to still parse as valid TOML.Notes
/404/. That is handled by Let hugo build the 404 page directly (#862) #1083 (which excludes the 404 page from the sitemap).netlify.toml; whichever merges second may need a trivial rebase, but there is no logical conflict.Sitemap:line added by Generate a robots.txt (#1068) #1080 (robots.txt) points at a valid sitemap.