[RESOLVED] absURL, baseURL and deploy previews

I currently use Netlify to deploy my website. I have a staging and a master branch. The master branch gets published automatically and I like to preview the staging branch (and any other spike branches that I have). If I set the baseURL to my domain and use absURL whenever I refer to a file (eg. img/featured_image.png) it uses the baseURL instead of the preview url.

One way to solve this would be to use relURL instead of absURL but there seem to be various reasons why it is better to have an absURL.

Another way would be to set the baseURL to “/” but then I can’t share my content properly on social media because the URL is not found.

How do you all deal with this?

Thanks,

David


In the Netlify deploy config, you can specify the --baseURL.

So do hugo --baseURL=https://foo.com/ instead of just hugo in the Build field.

You can do the same via netlify.toml by setting contexts.

My netlify.toml example


The build.sh in that example is a simple shellscript wrapper around hugo + my poor man’s asset optimization pipeline.

2 Likes

Probably cleaner to set HUGO_BASEURL env var. See netliy.toml in hugoDocs repo.

Probably cleaner to set HUGO_BASEURL env var. See netliy.toml in hugoDocs repo.

Thanks!

To summarise (Please correct me if I am wrong):

HUGO_BASEURL is a Hugo environment variable that overrides baseURL in the config.toml.

With Netlify it is possible to do branch deploys. eg. https://branch–gohugoio.netlify.com/

This can be configured similarly to https://github.com/gohugoio/hugoDocs/blob/master/netlify.toml

1 Like

Yes, but I notice that in your title of this thread mentions “deploy previews”, and I have not thought too hard about those.

Which will give you an URL on the form

https://deploy-preview-259–gohugoio.netlify.com/

Which kind of works, but I suspect that is because the Docs site is all relative URLs.

I see https://www.netlify.com/docs/continuous-deployment/#deploy-contexts that they expose a

DEPLOY_PRIME_URL

For this, but I’m not sure if it’s possible to set

HUGO_BASEURL=${DEPLOY_PRIME_URL}

Or something.

1 Like

Yes, but I notice that in your title of this thread mentions “deploy previews”, and I have not thought too hard about those.

I wrote that before I knew about branch deploys :wink: