Correct BaseURL on Netlify previews

The above highlighted line fixes it, but as it is not obvious reading the Netlify docs (they have promised to improve this), I thought I should post this.

The DEPLOY_PRIME_URL contains the correct URL in pull request previews, but to make sure Hugo uses it for a deployment target, you need to pass it in as a argument to hugo exactly like this:

[context.deploy-preview]
command = "hugo -b $DEPLOY_PRIME_URL"

I have put exacty like this in bold, because I fumbled around with context.deploy-preview.build and context.deploy-preview.environment before Netlify told me it needed to be placed below context.deploy-preview.

12 Likes

Would this also work?

[context.deploy-preview.environment]
HUGO_BASEURL = "$DEPLOY_PRIME_URL"

Probably not, and that’s what you meant by exactly like this :slight_smile:

Let me put it this way: I spent a fair amount of time yesterday, some of it talking to Netlify on a chat, trying out different variations. So when I say exactly this then I really mean it.

1 Like

I spent some time playing with my Netlify config and here’s what I came up with…

I discovered that you can specify the Hugo version just once:

[build.environment]
  HUGO_VERSION = "0.36"

And I ended up inlining the baseurl env. I like it as a param so I’ll probably swap that over when I get a chance.

2 Likes

Spent some time trying to debug why absolute links on Netlify aren’t generated. As it turned out, if you have baseURL = "" inside config.toml, then you also need to have these lines inside netlify.toml:

[build]
publish = "public"
command = "hugo -b $URL"

Without -b $URL it just doesn’t work, although I have command = "hugo -b $DEPLOY_PRIME_URL" as recommended in this thread.

1 Like

I also use this for deploy previews, because it will use the hashed URL of a specific deploy:

command = "hugo -b $DEPLOY_URL"

1 Like