Image doesn't load when visiting content from home page

I have some content under the following directory structure:

content/posts/system-software-from-developer-apple-inc.-has-been-updated/
    index.md
    images/
        combined.png
        ...

In system-software-from-developer-apple-inc.-has-been-updated/index.md I reference the above image with ![image text](images/combined.png).

I have found consistently when I have “freshly” loaded my home page if I click on the link to the content above, the image doesn’t load because the url to the image is relative to my site’s root rather than the content page. However, if I refresh the content page (or visit it directly by copy/pasting the url), the image url is correct and the image loads.

Just reviewing the Page Bundles docs as I write this: is it the case that page resources must be in the same directory as the page itself? That is, in my case the images for my post cannot be in the images/ subdirectory, rather, they must be in the same directory as index.md?

Details:

  • My home page
  • The post in question
  • My site’s Github repo
  • Local (macOS 11.6.4): hugo v0.93.2+extended darwin/amd64 BuildDate=unknown
  • Github Build: hugo v0.93.3-44E3C002+extended linux/amd64 BuildDate=2022-03-08T10:15:28Z VendorInfo=gohugoio
  <base href="{{ .Permalink }}">

is your problem. It says that all URLs are to be relative to this base href.

For further info:

@cshoredaniel - I don’t understand. When I navigate to the post, the value of <base href="{{ .Permalink }}"> updates to the URL of the post, which is what the base URL should be for the relative path images/combined.png that is in the post:

<base href="https://delu.co/posts/system-software-from-developer-apple-inc.-has-been-updated/">

The full URL to the image is https://delu.co/posts/system-software-from-developer-apple-inc.-has-been-updated/images/combined.png.

@Mattt Sorry, I was confusing what you had with the usual of setting of <base href="{{ site.baseurl }}">.

I wonder if this is actually a browser issue (base href is usually the same for pages in the same site or sub-site, not different every page, AIUI). I don’t see anything in the spec that would suggest it is supposed to matter, but I suspect this is a rather unique way of using the <base> element (but I am not a frontend developer).

Hopefully someone can chime in who notices something I missed, or has a deeper knowledge of the spec and expected behaviour.

1 Like

@Mattt

I cannot reproduce the problem. The image is present on your live site, and when I build the site locally.

@cshoredaniel

I’ve seen theme authors use the base element in an attempt to handle sites that are served from a subdirectory. But with the advent of link and render hooks, setting a base element causes more problems than it solves.

2 Likes

@cshoredaniel @jmooring - thanks for your insight on this one. Removing the <base> tag seems to have done the trick without any negative side effects. I think I’ll leave it at that.

@jmooring - the behaviour I would see is that the image would load after I reload the page. If I went back to my home page, reloaded the home page, then visited the post again, I’d see a broken link to the image until I refreshed the post.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.