加载时间分析与优化
7f8dedc4df48e4486b3697aeedd31dbe
优化前
{{define "DBHtmlCode"}}
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>{{.data.title}}</title>
<link rel="stylesheet" href="/static/WeUI_files/weui.css">
<link rel="stylesheet" href="/static/WeUI_files/example.css">
</head>
<body>
<div id="c"></div>
</body>
</html>
<script>
document.getElementById("testdiv").innerHTML = '<h1>{{.data.title}}</h1>{{.data.content}}';
let imgNodes = document.getElementsByTagName("img")
let len=imgNodes.length
for (let i=0;i<len; i++) {
console.log(i)
imgNodes[i].setAttribute("style", " width:"+window.screen.availWidth+"px")
}
</script>
{{end}}
package main
import (
"net/http"
"github.com/labstack/echo"
"io"
"html/template"
"fmt"
"io/ioutil"
//"encoding/json"
"encoding/json"
)
/*
1.实现 echo.Renderer 接口
*/
type Template struct {
templates *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
/*
4.在 action 中渲染模板
*/
func Hello(c echo.Context) error {
return c.Render(http.StatusOK, "WeUI", "chkUrl")
}
/*
自定义一个 context
Define a custom context
Context - Go/Golang 框架 Echo 文档 http://go-echo.org/guide/context/
*/
type CustomContext struct {
echo.Context
}
func (c *CustomContext) Foo() {
println("foo")
}
func (c *CustomContext) Bar() {
println("bar")
}
type ArticleContent struct {
title string `json:"title"`
summary string `json:"summary"`
content string `json:"content"`
modify_time int `json:"modify_time"`
}
type ArticleResponse struct {
status int `json:"int"`
data ArticleContent `json:"data"`
}
//
//var s2 = ScriptStruct{"192.168.3.123", 8088, "/myDir/", "spider.go"}
//var ScriptArr [6]ScriptStruct
//var DBHtmlCode ArticleResponse
//var DBHtmlCode string
var DBHtmlCode map[string]interface{}
//func (c *CustomContext) DumpScripts() {
// println("bar")
// s1 := ScriptStruct{"192.168.3.123", 8088, "/myDir/", "spider.go"}
// ScriptArr[0] = ScriptStruct{"192.168.3.103", 8088, "/myDir/", "spider.go"}
// ScriptArr[1] = ScriptStruct{"192.168.3.110", 8088, "/myDir/", "spider.go"}
// ScriptArr[2] = ScriptStruct{"192.168.3.123", 8088, "/myDir/", "spider.go"}
// fmt.Println(s1.Host)
//}
func (c *CustomContext) GetDBHtmlCode(info_id string) {
/*
The client must close the response body when finished with it:
*/
url := "http://47.98.201.96:8000/index/help/doc/?info_id=" + info_id
//fmt.Println(url)
resp, err := http.Get(url)
if err != nil {
// handle error
fmt.Println(err)
}
//fmt.Println(resp)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
fmt.Println(err)
}
//fmt.Println(body)
//println("json:", string(body))
//
//var a ArticleResponse
//
//if err = json.Unmarshal(body, &a); err != nil {
//
//}
//fmt.Println(a)
//var u ArticleResponse
//
//json.NewDecoder(body).Decode(&u)
//fmt.Println(u)
////fmt.Println(resp)
//var m ArticleResponse
////json.Unmarshal(body, &m)
//json.Unmarshal(body, &m)
//fmt.Println(m)
//
//var n ArticleResponse
//json.NewDecoder(resp.Body).Decode(&n)
//fmt.Println(n)
var dat map[string]interface{}
//var dat map[string]ArticleResponse
if err := json.Unmarshal(body, &dat); err != nil {
panic(err)
}
//fmt.Println(dat)
//fmt.Println(dat["status"])
//fmt.Println(dat["data"])
//fmt.Println(dat["content"])
//
//if err != nil {
// // handle error
// fmt.Println(err)
//
//}
DBHtmlCode = dat
//DBHtmlCode.status = 3
}
func main() {
/*
2.预编译模板
*/
t := &Template{
templates: template.Must(template.ParseGlob("goEchopublic/views/*.html")),
}
/*
3.注册模板
*/
e := echo.New()
e.Renderer = t
/*
静态文件
Echo#Static(prefix, root string) 用一个 url 路径注册一个新的路由来提供静态文件的访问服务。root 为文件根目录。
这样会将所有访问/static/*的请求去访问assets目录。例如,一个访问/static/js/main.js的请求会匹配到assets/js/main.js这个文件。
*/
e.Static("/static", "assets")
/*
创建一个中间件来扩展默认的 context
Create a middleware to extend default context
*/
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := &CustomContext{c}
return h(cc)
}
})
/*
这个中间件要在所有其它中间件之前注册到路由上。
This middleware should be registered before any other middleware.
*/
/*
在业务处理中使用
Use in handler
*/
e.GET("/scriptAdmin", Hello)
e.GET("/GetDBHtmlCode/:iid", func(c echo.Context) error {
info_id := c.Param("iid")
cc := c.(*CustomContext)
cc.GetDBHtmlCode(info_id)
return c.Render(http.StatusOK, "DBHtmlCode", DBHtmlCode)
})
e.Logger.Fatal(e.Start(":1323"))
}
YUI Compressor http://yui.github.io/yuicompressor/
java -jar yuicompressor-2.4.8.jar --charset utf-8 --type css assets/WeUI_files/weui.css -o assets/WeUI_files/weui-yuiCompressor.css
压缩后