gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.96k stars 7.54k forks source link

Base template slots not filled on 404 page #13068

Closed Cynosure-North closed 23 minutes ago

Cynosure-North commented 3 hours ago

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.139.0-dacef0df92a9a20072b4eaa42003e1a63ace8f41+extended linux/amd64 BuildDate=2024-11-18T16:17:45Z VendorInfo=hugomods

I've bisected with past releases and this error seems to have been introduced in release 0.123.0

Does this issue reproduce with the latest release?

Yes

Issue

Hugo does not use the base template overrides for 404 pages, resulting in missing or incomplete pages.

The example below shows the define block being ignored.

layouts/404.html

<h1>404 example</h1>

{{ define "main" }}
  <h1>404 Not Found</h1>
  <p>The page you requested cannot be found.</p>
  <p>
    <a href="{{ .Site.Home.RelPermalink }}">
      Return to the home page
    </a>
  </p>
{{ end }}

{{ if eq 1 1 }}
  <p>true</p>
{{ else }}
  <p>false</p>
{{ end }}

public/404.html

<h1>404 example</h1>

  <p>true</p>

\ \ This example shows there is no 404.html generated in the public folder

layouts/404.html

{{ define "main" }}
  <h1>404 Not Found</h1>
  <p>The page you requested cannot be found.</p>
  <p>
    <a href="{{ .Site.Home.RelPermalink }}">
      Return to the home page
    </a>
  </p>
{{ end }}
jmooring commented 3 hours ago

https://gohugo.io/templates/base/#override-the-base-template

Code that you put outside the block definitions can break your layout. This even includes HTML comments. For example...

bep commented 23 minutes ago

See @jmooring 's comment. This is unfortunate, but this is a limitation of Go templates, and not something we can fix.