.Page.Ressources.Get is NIL on automatic rebuild

Hello,

I have the following page bundles:

├── content
│   ├── posts
│   │   ├── post1
│   │   │   ├── image1.jpg
│   │   │   └── index.md
│   │   ├── post2
│   │   │   ├── image2.jpg
│   │   │   └── index.md
│   │   ├── _index.md

As section page I want to concatenate all posts including images. The section.html is:

{{ define "main" }}
  <div class="header">
    {{ .Content }}
  </div>
  {{ range .Pages }}
    <div class="post">
      {{ .Title }}
      {{ .Content }}
    </div>
  {{ end }}
{{ end }}

I wrote a simple shortcode to include images from page bundles within markdown:

  {{- $u := urls.Parse (.Get "path") -}}
  {{- $src := $u.String -}}
  {{- $rec := .Page.Resources.Get ($u.Path) -}}
  {{- $src = $rec.RelPermalink -}}

  <img style="width: 100%" src="{{ $src }}"></img>

To include the image I’m using the shortcode in e.g. post1/index.md:

{{< image path=image1.jpg >}}

This works as expected when building the page.

However, when I run hugo server or hugo -w and add a new post as page bundle, hugo complains that the image does not exist (.Page.Resources.Get ($u.Path) is NIL).

When restarting hugo, everything is rendered as expected.

Why is it rendered correctly on first run, but not when changes are detected?

First question… why do you need a shortcode instead of just using Markdown?

You are right, the Markdown syntax works as expected (also on rebuilds).

I introduced the shortcode as I want to add additional styling like width: 100%. And I also created a shortcode for local videos.

I wonder what the difference between my shortcode and the native Markdown is.

It tried the embedded figure shortcode. With this I have the same behaviour: If content is available on hugo server start, the image is shown correctly. If the content is added during running hugo, the image is not shown.

The generated html page differs.

Working code after hugo restart:

<figure class="ma0 w-75"><a href="https://www.nps.gov/zion/index.htm"><img src="/posts/figure_test/image.jpg"
    alt="A photograph of Zion National Park"></a><figcaption>
      <p>Zion National Park</p>
    </figcaption>
</figure>

Not working solution after automatic rebuild:

<figure class="ma0 w-75"><a href="https://www.nps.gov/zion/index.htm"><img src="image.jpg"
    alt="A photograph of Zion National Park"></a><figcaption>
      <p>Zion National Park</p>
    </figcaption>
</figure>

The src content differ. This means that .RelPermalink is calculated incorrectly.

Starting with the example below, please provide the steps to reproduce the problem:

git clone --single-branch -b hugo-forum-topic-55737 https://github.com/jmooring/hugo-testing hugo-forum-topic-55737
cd hugo-forum-topic-55737
hugo server

Thanks a lot for that simple example. with this it is very easy to reproduce:

hugo server

On another terminal

cp -r content/posts/post-3 content/posts/post-4

Now there is an addition entry on the webpage without the kitten.

When restarting hugo server and reloading the page, everything is as expected.

I also created a screen record, but don’t know how to share that video.

It looks as if hugo server isn’t picking up the new file. Perhaps an issue with the file system you’re using.

Aside: A white-on-black screen shot (or a dark one with a **bolder **font) would be easier to read.

There are several known issues related to the embedded server. This may be a variant of one of those.

I suggest you create a new issue, cross-referencing this forum topic.

If I were you, I would insert the images using Markdown, and style the images with CSS instead of inline styles which are often blocked by CSPs.

@jmooring Thanks for the suggestions. Using markdown style images is a good idea, but I still need a shortcode for videos.

I’m going to create a bug ticket. Do you have similar behavior on your side or may it really depend on the file system as @chrillek mentions?

For completeness a video (but the same color scheme):

animated

Yes, I can easily reproduce using the instructions you provided.

Ticket created: .Page.Ressources.Get is NIL on automatic rebuild · Issue #13925 · gohugoio/hugo · GitHub

1 Like

This will be fixed in Fix server rebuild when adding a new leaf bundle with resources in one go by bep · Pull Request #13928 · gohugoio/hugo · GitHub

I’m doing a Hugo release later tonight (Norway time) to upgrade Hugo to the new Go 1.25 versio, and I will merge this before that.

1 Like

Thanks @bep, that is very quick.

Just to make sure: The same code (and thus patch) applies to hugo server and hugo -w?

Yes it is.

Just a heads up: I’m not releasing until Monday (the Go 1.25 upgrade was trickier than I thought).

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