Jekyll site uses absolute urls; deploy previews navigate to main site

I’m new to both netlify and jekyll (and static site generators in general). I’ve got my site nearly built (see https://angry-tereshkova-959566.netlify.com/.

Now I want to test my forms, without consuming lots of paid form submissions. I created a brand and a PR in GitHub, and netlify created a deploy-preview at https://5e3a236b37e50a000827cfa9–angry-tereshkova-959566.netlify.com

But my template (Feeling Responsive) makes some absolute references; it defines url in _config.yml and references {site.url} in some places.

Is there an easy fix for this?

I read Incorect url domain prefix in build preview links, but that applies to Hugo and I did not understand how to interpret it for my situation.

Hi, @bolaurent. Welcome to our Netlify community site.

There is a --baseurl command line flag which I think (I’m not a Jekyll expert) works the same way. I found this flag in the documentation here:

So, I believe a build command below might work for deploy previews (in netlify.toml):

[context.deploy-preview]
  command = "jekyll build --baseurl  $DEPLOY_PRIME_URL"

Would you be willing to please test that and let us know if it solves this issue?

I haven’t tried that, but I don’t think it will work. See Clearing Up Confusion Around baseurl – Again | By Parker. What needs to be changed is “url”, not “baseurl”.

I am managing the url difference between local dev and netlify by loading two _config.yml files, with the first specifying url: ‘https://angry-tereshkova-959566.netlify.com’, and the second overwrites that with url: ‘http://localhost:4000’. This doesn’t work for a preview because I cannot predict the url.

jekyll build --config _config.yml,_config_dev.yml --watch

With the hugo example, it allows the passing of that setting via the command build command and Netlify provides an environment variable which can be used in our build environment.

I don’t know if there is a way to do this with jekyll or not. One workaround would be to modify the jekyll config file and insert this environment variable ($DEPLOY_PRIME_URL) into that YAML file during the build.

This might be done using a two part build command:

  • a script that programmatically inserts the $DEPLOY_PRIME_URL value into the YAML file
  • the actually build command

For example:

script-that-inserts.sh && jekyll build

Another solution is to modify the theme to use absolute URLs for other assets.

That seems helpful; I’ll give it a try and report back on how it works out.

Thanks Luke

The technique I am using to run locally is to load the main _config.yml file, followed by a _config_dev.yml file that overwrites the site parameter.

I think I just need to have the build script write a one-line yml file that only sets site, and consume it last.

_config.yml:

url: 'https://angry-tereshkova-959566.netlify.com'

_config_dev.yml:

url: 		'http://localhost:4000'

So arg, I’ve been googling and asking, and for some reason a lot of people want to have access to env vars in jekyll but no one seems to be succeeding.

It seems like if I can run a line in CLI that creates a temp _conf_build.yml file I can load it and overwrite site.url. I’ll try to learn about that now. As a newbie, I have been able to use netlify with almost zero learning; I just made a local jekyll site, pushed it to a repo, and bam! netlify built and served it for me. So nice.

Ok, so if I can figure out how to get the netlify build to do this (this is bash):

printf "site: %s" "$DEPLOY_PRIME_URL" > _config_netlify.yml

and then run jekyll like this:

bundle exec jekyll server --config _config.yml,_config_netlify.yml

I’ll get what I need.

Next I have to study build options in netlify.

Is there a file system and current working directory at the moment of deploy?

1 Like

Answering my own question, the following works:

printf "site: %s" "$DEPLOY_URL" > _config_netlify.yml; jekyll build --config _config.yml,_config_netlify.yml
1 Like

Thanks for sharing your solution! That might really help someone else :slight_smile:

I had to tweak this a little to make it work in my setup.
netlify.toml:

[context.deploy-preview]
  command = "printf \"url: %s\" \"$DEPLOY_PRIME_URL\" > _config_netlify.yml; jekyll build --config _config.yml,_config_netlify.yml"

Uses ‘url’ rather than ‘site’.
DEPLOY_PRIME_URL is the ‘pretty’ url, DEPLOY_URL works too, but is not the same as the review app one. - Build environment variables | Netlify Docs

thank you for sharing!