mirror of
1
Fork 0

Fix load time bug (#14508)

This commit is contained in:
Lunny Xiao 2021-01-29 12:33:47 +08:00 committed by GitHub
parent aec8029277
commit f761c82c94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"html"
"html/template"
"io"
@ -182,6 +183,10 @@ func (ctx *Context) RedirectToFirst(location ...string) {
// HTML calls Context.HTML and converts template name to string.
func (ctx *Context) HTML(status int, name base.TplName) {
log.Debug("Template: %s", name)
var startTime = time.Now()
ctx.Data["TmplLoadTimes"] = func() string {
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
}
if err := ctx.Render.HTML(ctx.Resp, status, string(name), ctx.Data); err != nil {
ctx.ServerError("Render failed", err)
}
@ -190,6 +195,10 @@ func (ctx *Context) HTML(status int, name base.TplName) {
// HTMLString render content to a string but not http.ResponseWriter
func (ctx *Context) HTMLString(name string, data interface{}) (string, error) {
var buf strings.Builder
var startTime = time.Now()
ctx.Data["TmplLoadTimes"] = func() string {
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
}
err := ctx.Render.HTML(&buf, 200, string(name), data)
return buf.String(), err
}
@ -547,10 +556,7 @@ func Contexter() func(next http.Handler) http.Handler {
Data: map[string]interface{}{
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"PageStartTime": startTime,
"TmplLoadTimes": func() string {
return time.Since(startTime).String()
},
"Link": link,
"Link": link,
},
}