Ir al contenido

HTTP/2 Server Push

HTTP/2 server push permite que el servidor envíe recursos al cliente antes de que se soliciten, eliminando un viaje de ida y vuelta para assets que la página sabe que necesitará. Esta receta envía el CSS, JavaScript e imagen de una página junto con la response HTML.

1. Registrar una ruta para servir web assets

Sección titulada «1. Registrar una ruta para servir web assets»
e.Static("/", "static")

2. Servir index.html y hacer push de sus dependencias

Sección titulada «2. Servir index.html y hacer push de sus dependencias»

Desenvuelve la response para acceder al http.ResponseWriter subyacente, y luego haz push de cada asset si el writer implementa 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")
})
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)
}
<!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>
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)
}
}