aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-07-17 11:10:46 +0200
committerBjørn Erik Pedersen <[email protected]>2024-07-17 12:47:25 +0200
commitf0ed91caba9ab962593479e745a0df1a5518c4bf (patch)
tree6356a09cc3ce704016a4279b2d70aac1d6a6a536 /tpl
parent7be03775051371df88dc0400cccc20be5e9632c8 (diff)
downloadhugo-f0ed91caba9ab962593479e745a0df1a5518c4bf.tar.gz
hugo-f0ed91caba9ab962593479e745a0df1a5518c4bf.zip
Throw error if resources.PostProcess is used in a deferred template
That just doesn't work. See #12655
Diffstat (limited to 'tpl')
-rw-r--r--tpl/templates/defer_integration_test.go20
-rw-r--r--tpl/tplimpl/template_ast_transformers.go8
2 files changed, 27 insertions, 1 deletions
diff --git a/tpl/templates/defer_integration_test.go b/tpl/templates/defer_integration_test.go
index 2c2bf0d80..8b06e7722 100644
--- a/tpl/templates/defer_integration_test.go
+++ b/tpl/templates/defer_integration_test.go
@@ -200,3 +200,23 @@ func TestDeferFromContentAdapterShouldFail(t *testing.T) {
b.Assert(err, qt.Not(qt.IsNil))
b.Assert(err.Error(), qt.Contains, "error calling Defer: this method cannot be called before the site is fully initialized")
}
+
+func TestDeferPostProcessShouldThrowAnError(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+-- assets/mytext.txt --
+ABCD.
+-- layouts/index.html --
+Home
+{{ with (templates.Defer (dict "key" "foo")) }}
+{{ $mytext := resources.Get "mytext.txt" | minify | resources.PostProcess }}
+{{ end }}
+
+`
+ b, err := hugolib.TestE(t, files)
+
+ b.Assert(err, qt.Not(qt.IsNil))
+ b.Assert(err.Error(), qt.Contains, "resources.PostProcess cannot be used in a deferred template")
+}
diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go
index ab6cf7b07..57730f82e 100644
--- a/tpl/tplimpl/template_ast_transformers.go
+++ b/tpl/tplimpl/template_ast_transformers.go
@@ -16,6 +16,7 @@ package tplimpl
import (
"errors"
"fmt"
+ "strings"
"github.com/gohugoio/hugo/helpers"
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
@@ -248,7 +249,12 @@ func (c *templateContext) handleDefer(withNode *parse.WithNode) {
n := l.Nodes[0].(*parse.ActionNode)
inner := withNode.List.CopyList()
- innerHash := helpers.MD5String(inner.String())
+ s := inner.String()
+ if strings.Contains(s, "resources.PostProcess") {
+ c.err = errors.New("resources.PostProcess cannot be used in a deferred template")
+ return
+ }
+ innerHash := helpers.MD5String(s)
deferredID := tpl.HugoDeferredTemplatePrefix + innerHash
c.deferNodes[deferredID] = inner