Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Add netlifly URL aware build (fixes sitemaps and absolute urls in Hugo) #532

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sergioisidoro
Copy link

@sergioisidoro sergioisidoro commented Sep 17, 2021

- Summary

There is a problem-ish with Hugo itself, as specified here: https://discourse.gohugo.io/t/invalid-sitemap-url/26269/3

If you have

baseurl = "/"

Hugo will generate a sitemap that does not have valid URLs, according to the sitemap standard. This is problematic if you want to add your site to Google search index.

If you do

baseurl = "your.base.url.com"

Then all your dev environment links will link to your production site, which is not great.

Other templates added their own magic to their build process, as is the example of Atlas template: https://github.com/indigotree/atlas/pull/39/files#diff-7291edac743a0e6f594d817cb46f00cb3866bf89f40569ae494e36c1b722afa8R65

In this approach I leveraged the Netlify build env and just bash sh:

Netlifly has a build env DEPLOY_PRIME_URL that contains the main url of the site. By passing that env variable in the build process (defaulting to /) we are able to generate valid site maps with the default templates.

Warranted that the default value might generate some confusion if they change baseUrl and the setting is overridden by the build process. Suggestions are welcome. Maybe making an if/elif would be better.

EDIT Added support for production and deploy builds

- Test plan

npm run build Generates:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>/about/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>
    <loc>/blog/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>

URL=hello.com DEPLOY_PRIME_URL=world.com CONTEXT=production npm run build
Generates

  <url>
    <loc>hello.com/about/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>
    <loc>hello.com/blog/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>

URL=hello.com DEPLOY_PRIME_URL=world.com CONTEXT=deploy-preview npm run build
Generates

  <url>
    <loc>world.com/about/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>
    <loc>world.com/blog/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>
    <loc>world.com/contact/</loc>
    <lastmod>2021-08-01T00:00:01+00:00</lastmod>
  </url><url>

- Description for the changelog

  • Fix default sitemaps to contain base url of production site during build

- A picture of a cute animal (not mandatory but encouraged)

@sergioisidoro sergioisidoro changed the title Add netlifly URL aware build Add netlifly URL aware build (fixes sitemaps and absolute urls in Hugo) Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant