Skip to content

sitemap.xml emits relative <loc> values, so search engines reject it #1082

Description

@dduugg

https://www.shift2bikes.org/sitemap.xml builds and serves fine (200), but every entry is a relative path:

<url>
  <loc>/pages/bike-summer/</loc>
</url>
<url>
  <loc>/archive/get-lit/</loc>
  <lastmod>2017-11-13T08:19:34+00:00</lastmod>
</url>

The sitemap protocol requires <loc> to be a fully qualified URL, so the 72 entries currently in the file are almost certainly being discarded.

Cause

site/config/_default/hugo.toml sets:

baseURL = "/"

Hugo builds <loc> from each page's .Permalink, which is derived from baseURL, so the permalinks come out relative.

This is not a one-line fix

baseURL = "/" is load-bearing rather than an oversight. The theme uses absURL for essentially every asset, and for the calendar's own API calls:

site/themes/s2b_hugo_theme/layouts/partials/cal/scripts.html:140:  url: '{{ absURL "api/events.php" }}',
site/themes/s2b_hugo_theme/layouts/partials/head-content.html:29:  <link rel="icon" href="{{ absURL "favicon.ico" }}">
site/themes/s2b_hugo_theme/layouts/partials/cal/up-next.html:1:  <script src="{{ absURL "lib/fullcalendar/core/main.min.js" }}"></script>

With baseURL = "/" these resolve same-origin, which is what lets the site work unchanged on localhost, on netlify deploy previews, and in production. Hardcoding the production URL into hugo.toml would make deploy previews and local dev load their scripts from, and fetch their event data from, www.shift2bikes.org.

Suggested approach

Set HUGO_BASEURL per netlify context rather than in hugo.toml, so each deploy gets the origin it is actually served from:

  • production: the canonical https://www.shift2bikes.org/
  • deploy previews and branch deploys: $DEPLOY_PRIME_URL
  • local dev (npm run dev, -e development): leave as /

Netlify exposes both URL and DEPLOY_PRIME_URL to the build, and hugo reads HUGO_BASEURL from the environment, so this can live in the command lines already present in netlify.toml.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions