HTTP/2 Server Push
HTTP/2 server push により、サーバーはリソースがリクエストされる前にクライアントへ送信できます。 ページが必要とすることが分かっているアセットの往復通信をなくせます。このレシピでは、HTML レスポンスと一緒に ページの CSS、JavaScript、画像をプッシュします。
1. Web アセットを配信するルートを登録する
Section titled “1. Web アセットを配信するルートを登録する”e.Static("/", "static")2. index.html を配信し、その依存関係をプッシュする
Section titled “2. index.html を配信し、その依存関係をプッシュする”レスポンスを unwrap して基底の http.ResponseWriter にアクセスし、writer が http.Pusher
を実装している場合に各アセットをプッシュします。
e.GET("/", func(c *echo.Context) (err error) { rw, err := echo.UnwrapResponse(c.Response()) if err != nil { return } if pusher, ok := rw.ResponseWriter.(http.Pusher); ok { if err = pusher.Push("/app.css", nil); err != nil { return } if err = pusher.Push("/app.js", nil); err != nil { return } if err = pusher.Push("/echo.png", nil); err != nil { return } } return c.File("index.html")})3. TLS サーバーを起動する
Section titled “3. TLS サーバーを起動する”sc := echo.StartConfig{Address: ":1323"}if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil { e.Logger.Error("failed to start server", "error", err)}ソースコード
Section titled “ソースコード”index.html
Section titled “index.html”<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTTP/2 Server Push</title> <link rel="stylesheet" href="/app.css"> <script src="/app.js"></script></head><body> <img class="echo" src="/echo.png"> <h2>The following static files are served via HTTP/2 server push</h2> <ul> <li><code>/app.css</code></li> <li><code>/app.js</code></li> <li><code>/echo.png</code></li> </ul></body></html>server.go
Section titled “server.go”package main
import ( "context" "net/http"
"github.com/labstack/echo/v5")
func main() { e := echo.New() e.Static("/", "static") e.GET("/", func(c *echo.Context) (err error) { rw, err := echo.UnwrapResponse(c.Response()) if err != nil { return } if pusher, ok := rw.ResponseWriter.(http.Pusher); ok { if err = pusher.Push("/app.css", nil); err != nil { return } if err = pusher.Push("/app.js", nil); err != nil { return } if err = pusher.Push("/echo.png", nil); err != nil { return } } return c.File("index.html") })
sc := echo.StartConfig{Address: ":1323"} if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil { e.Logger.Error("failed to start server", "error", err) }}