diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-09-07 15:07:10 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-09-07 21:06:44 +0200 |
commit | 4055c121847847d8bd6b95a928185daee065091b (patch) | |
tree | 6620f51e7e89aa7ff0a9a93361d640ee5b297fea /tpl/tplimpl/template_funcs.go | |
parent | 3ba7c92530a80f2f04fe57705ab05c247a6e8437 (diff) |
Fix some change detection issues on server reloads
* Fix change detection when .GetPage/site.GetPage is used from shortcode
* Fix stale content for GetPage results with short name lookups on server reloads
Fixes #7623
Fixes #7624
Fixes #7625
Diffstat (limited to 'tpl/tplimpl/template_funcs.go')
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index a688abb77..25ace365d 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -64,7 +64,8 @@ var _ texttemplate.ExecHelper = (*templateExecHelper)(nil) var zero reflect.Value type templateExecHelper struct { - funcs map[string]reflect.Value + running bool // whether we're in server mode. + funcs map[string]reflect.Value } func (t *templateExecHelper) GetFunc(tmpl texttemplate.Preparer, name string) (reflect.Value, bool) { @@ -91,14 +92,15 @@ func (t *templateExecHelper) GetMapValue(tmpl texttemplate.Preparer, receiver, k } func (t *templateExecHelper) GetMethod(tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) { - // This is a hot path and receiver.MethodByName really shows up in the benchmarks. - // Page.Render is the only method with a WithTemplateInfo as of now, so let's just - // check that for now. - // TODO(bep) find a more flexible, but still fast, way. - if name == "Render" { - if info, ok := tmpl.(tpl.Info); ok { - if m := receiver.MethodByName(name + "WithTemplateInfo"); m.IsValid() { - return m, reflect.ValueOf(info) + if t.running { + // This is a hot path and receiver.MethodByName really shows up in the benchmarks, + // so we maintain a list of method names with that signature. + switch name { + case "GetPage", "Render": + if info, ok := tmpl.(tpl.Info); ok { + if m := receiver.MethodByName(name + "WithTemplateInfo"); m.IsValid() { + return m, reflect.ValueOf(info) + } } } } @@ -133,7 +135,8 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec } exeHelper := &templateExecHelper{ - funcs: funcsv, + running: d.Running, + funcs: funcsv, } return texttemplate.NewExecuter( |