Relative urls: pipelines on page resources produce RelPermalinkabsolute

I am minimizing a .json page resource that get’s consumed by javascript on the page. That bit is working as expected in dev. But when I use the path to the minified resource (RelPermalink) in order ensure that it gets written to disk, the path becomes absolute.

In my config:

baseURL: ""
relativeURLs: true

(I’m deploying to an environment where the directory changes w/ each deployment, so baseURL CANNOT be used.)

What I would like to see is <div class="question" data-dataFile="data.min.json">. But instead I get <div class="question" data-dataFile="/section/subsection/page/data.min.json">.

                {{ $jsonMin := "" }}
                {{ range $jsonPathRes := .Page.Resources.Match ("*.json") }}
                    {{ if eq $jsonPathRes.Name "data.json" }}
                        {{ $jsonMin = $jsonPathRes | resources.Minify  }}
                    {{ end }}
                {{ end }}
                <div class="question" data-dataFile="{{$jsonMin.RelPermalink}}"></div>

What does work is simply lopping off the entire folder structure (data-dataFile="{{path.Base $jsonMin.RelPermalink}}"). But this feels brittle.

Is there a way (other than RelPermalink) to calculate a relative path to an asset, given a base url?

I would suggest that you use baseURL; you can set it as env or send it as a flag e.g. hugo -b https://mybaseurl.

Also, we don’t validate the baseURL, but a blank value isn’t valid.

Unfortunately, hard-coding a baseURL is simply not an option. I’m building and deploying to an LMS environment. So the courseware (Hugo website) gets placed into a newly created folder on S3, whose name changes w/each deployment, and cannot be predicted.

I’ve accepted that a side-effect is that I must always use RelPermaLink as absolute links cannot be calculated. Given the nature of the content (password protected, requires learner enrolment), it will probably not be linked to directly, nor will it ever be found by a search engine.

Or can I use something like ./ as baseURL? I guess another option would be to recalculate a “corrected” relative link path w/Javascript. Maybe use Javascript to calculate where the site install begins, and then use that to write a <meta base-href="">?

Hello. Also jumping in on this topic as I am encountering the exact same issue as @Scott_Talsma. Apologies ahead for the thread necromancy.

In our case we host a series of versioned documentation on a single server, each version generated with Hugo, with the version part of the URL. Our server also knows of a few “symlinks” that redirect to a dynamic version (think latest directing to the latest uploaded version). When a user navigates to the documentation via such a symlink we don’t want clicking on a link internal to those docs to change the base URL, hence we only want to use relative links.

@bep in a more general manner I would be keen to understand why a field and methods with names such as RelPermalink don’t return a relative link when there’s also a dedicated PermaLink version. Not a rhetorical question at all as I am relatively new to Hugo and might be missing something essential. I would be keen on learning more. :slightly_smiling_face: