Symlinked post images are turned into unique files after building the site

I have several images that I use as background of post headings. Those images are shared between several posts on the same subject. I try to use symlinks as much as possible, and Hugo does indeed follow them when it’s building the site.

However, after the site is built, those symlinks are turned into unique files. So, as you can imagine, Hugo leaves me with duplicates of the same images in different locations. This is unfortunate, because a visitor will be downloading files with the same content multiple times, instead of getting the file from his browser cache for each linked instance.

What is the preferred solution to this problem? If possible, I’d like to avoid putting those files into /static/images.

Hugo duplicates these files because symlinks in different page bundles are treated as new files.

Also you should not rely on symlinks in a project as they can break easily and are not very portable -yes, even relative ones-.

Instead use a front matter parameter to call a file that is published through another Page Bundle.


linked = "/work/example/some.webp"

And then use it in your template, like so:


{{ with .Params.linked }}<--- HTML --->{{ end }}

This is a very simple way. I’m sure others might have more sophisticated ways, yet it does the job and it is far easier to maintain than symlinks.

Thanks! I’ll test it out ASAP and report back in a couple of days.