Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time

+++ title = "HTTP/2 Server Push Recipe" description = "HTTP/2 server push recipe for Echo" [menu.main] name = "HTTP/2 Server Push" parent = "cookbook" +++

Requires go1.8+

How to send web assets using HTTP/2 server push?

Step 1: Generate a self-signed X.509 TLS certificate

Step 2: Register a route to serve web assets

e.Static("/", "static")

Step 3: Create a handler to serve index.html and push it's dependencies

e.GET("/", func(c echo.Context) (err error) {
  pusher, ok := c.Response().Writer.(http.Pusher)
  if ok {
    if err = pusher.Push("/app.css", nil); err != nil {
    if err = pusher.Push("/app.js", nil); err != nil {
    if err = pusher.Push("/echo.png", nil); err != nil {
  return c.File("index.html")

If http.Pusher is supported, web assets are pushed; otherwise, client makes separate requests to get them.

Step 4: Configure TLS server using cert.pem and key.pem

e.StartTLS(":1323", "cert.pem", "key.pem")

Step 5: Start the server and browse to https://localhost:1323

Protocol: HTTP/2.0
Host: localhost:1323
Remote Address: [::1]:60288
Method: GET
Path: /

[Source Code]({{< source "http2-server-push" >}})


{{< embed "http2-server-push/index.html" >}}


{{< embed "http2-server-push/server.go" >}}