Fast render and .Scratch issue

I’ve found a strange behaviour of hugo server (the issue doesn’t show up when I deploy the site) and the .Scratch feature. I have two pages:

  • index.html
  • 404.html

The 404.html just has this code:

{{- with .Site.GetPage "/" -}}
    {{- /* Notify that this is 404 page */ -}}
    {{- .Scratch.Set "is_404" true -}}
    {{- .Render -}}
{{- end -}}

The index.html page just checks whether “is_404” is set or not in order to show a “Page not found” message:

{{ if .Scratch.Get "is_404" }}
    <h2>Page not found</h2>
{{ end }}

When I spawn the server and navigate to index.html, the “Page not found” message is shown when it shoudn’t. If, while the server is running, I make a dummy edit on any of the source files and save changes, then the server successfully delivers index.html without the “Page not found” message.

I’ve found out that the issue disappears if I disable the “fast render” feature of Hugo:

hugo server --disableFastRender

It’s not a critical issue as the problem doesn’t affect page deployment, but just wanted to point out this glitch.

Wouldn’t this always run, since presumably you’ll always have a homepage?

Would just placing the below text in your 404.html template not give you the behavior you want?

<h2>Page not found</h2>

AFAIK it would run only if “404.html” were requested. Furthermore when I generate the site, the resulting “404.html” file is exactly the same as “index.html” with the single difference of that line with the “Page not found” message, as expected.

In any case, if you are right, the hugo server would always return the same result but it doesn’t: as soon as I edit and save some file (by just adding a dummy space for example) the server returns the expected “index.html” page without the “page not found message”.

When running hugo server, for whatever reason it doesn’t use the 404 template.

But, your 404 page will behave as expected on other web servers. Here’s a quick way to test, using Node’s http-server package:

  1. cd your-hugo-site
  2. hugo
  3. If not already installed, then install Node JS
  4. npm install http-server -g
  5. http-server ./public -p 1414
  6. Navigate to http://localhost:1414/some-page-that-does-not-exist and your 404 page will be served

Yeah, as I mentioned before I already checked the published site works (in my case I use Apache). The funny thing is that although Hugo doesn’t render the 404 page, somehow it’s injecting the 404 template code when I’m actually requesting “index.html”! :slight_smile:

Anyway, by disabling the fast render feature I get rid of this glitch.

My guess is somehow that Scratch variable is being set to true initially, then changed whenever you make your first edit. But glad you got it resolved.

1 Like