8000 server: Refresh the error template · gohugoio/hugo@657d1a2 · GitHub < 8000 link rel="manifest" href="/manifest.json" crossOrigin="use-credentials">
[go: up one dir, main page]

Skip to content

Commit 657d1a2

Browse files
committed
server: Refresh the error template
We cannot cache it forever, as that will not allow the end user to edit and preview it.
1 parent 87a22eb commit 657d1a2

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

commands/server.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535

3636
"github.com/gohugoio/hugo/common/htime"
3737
"github.com/gohugoio/hugo/common/paths"
38+
"github.com/gohugoio/hugo/hugolib"
39+
"github.com/gohugoio/hugo/tpl"
3840
"golang.org/x/sync/errgroup"
3941

4042
"github.com/gohugoio/hugo/livereload"
@@ -522,20 +524,37 @@ func (c *commandeer) serve(s *serverCmd) error {
522524
roots = []string{""}
523525
}
524526

525-
templHandler := c.hugo().Tmpl()
526-
errTempl, found := templHandler.Lookup("_server/error.html")
527-
if !found {
528-
panic("template server/error.html not found")
527+
// Cache it here. The HugoSites object may be unavaialble later on due to intermitent configuration errors.
528+
// To allow the en user to change the error template while the server is running, we use
529+
// the freshest template we can provide.
530+
var (
531+
errTempl tpl.Template
532+
templHandler tpl.TemplateHandler
533+
)
534+
getErrorTemplateAndHandler := func(h *hugolib.HugoSites) (tpl.Template, tpl.TemplateHandler) {
535+
if h == nil {
536+
return errTempl, templHandler
537+
}
538+
templHandler := h.Tmpl()
539+
errTempl, found := templHandler.Lookup("_server/error.html")
540+
if !found {
541+
panic("template server/error.html not found")
542+
}
543+
return errTempl, templHandler
529544
}
545+
errTempl, templHandler = getErrorTemplateAndHandler(c.hugo())
530546

531547
srv := &fileServer{
532548
baseURLs: baseURLs,
533549
roots: roots,
534550
c: c,
535551
s: s,
536552
errorTemplate: func(ctx any) (io.Reader, error) {
553+
// hugoTry does not block, getErrorTemplateAndHandler will fall back
554+
// to cached values if nil.
555+
templ, handler := getErrorTemplateAndHandler(c.hugoTry())
537556
b := &bytes.Buffer{}
538-
err := templHandler.Execute(errTempl, b, ctx)
557+
err := handler.Execute(templ, b, ctx)
539558
return b, err
540559
},
541560
}

0 commit comments

Comments
 (0)
0