Skip to content

HTTP/2 Server Push

Learn how to implement HTTP/2 Server Push in Iris.

Basic Server Push

go
package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()

    app.Get("/", func(ctx iris.Context) {
        if pusher := ctx.ResponseWriter().Pusher(); pusher != nil {
            // Push CSS file
            if err := pusher.Push("/assets/style.css", nil); err != nil {
                ctx.Application().Logger().Warnf("Failed to push: %v", err)
            }
            
            // Push JavaScript file
            if err := pusher.Push("/assets/app.js", nil); err != nil {
                ctx.Application().Logger().Warnf("Failed to push: %v", err)
            }
        }

        ctx.View("index.html")
    })

    app.Listen(":8080", iris.WithOptimizations)
}

Advanced Push Options

go
app.Get("/", func(ctx iris.Context) {
    if pusher := ctx.ResponseWriter().Pusher(); pusher != nil {
        opts := &http.PushOptions{
            Header: http.Header{
                "Accept-Encoding": ctx.Request().Header["Accept-Encoding"],
            },
            Method: "GET",
        }

        assets := []string{
            "/assets/css/style.css",
            "/assets/js/app.js",
            "/assets/images/logo.png",
        }

        for _, asset := range assets {
            if err := pusher.Push(asset, opts); err != nil {
                ctx.Application().Logger().Warnf("Failed to push %s: %v", asset, err)
            }
        }
    }

    ctx.View("index.html")
})

Conditional Push

go
app.Get("/", func(ctx iris.Context) {
    if !ctx.ClientSupportsHTTP2() {
        ctx.View("index.html")
        return
    }

    if pusher := ctx.ResponseWriter().Pusher(); pusher != nil {
        // Push critical resources first
        criticalAssets := []string{
            "/assets/css/critical.css",
            "/assets/js/critical.js",
        }

        for _, asset := range criticalAssets {
            if err := pusher.Push(asset, nil); err != nil {
                ctx.Application().Logger().Warnf("Failed to push %s: %v", asset, err)
            }
        }

        // Push non-critical resources
        go func() {
            nonCriticalAssets := []string{
                "/assets/css/non-critical.css",
                "/assets/js/non-critical.js",
            }

            for _, asset := range nonCriticalAssets {
                if err := pusher.Push(asset, nil); err != nil {
                    ctx.Application().Logger().Warnf("Failed to push %s: %v", asset, err)
                }
            }
        }()
    }

    ctx.View("index.html")
})

Best Practices

  1. Implementation:

    • Push critical resources
    • Check client support
    • Handle errors
    • Monitor pushes
    • Clean resources
  2. Performance:

    • Push selectively
    • Prioritize resources
    • Monitor impact
    • Handle timeouts
    • Regular cleanup
  3. Headers:

    • Set proper headers
    • Handle encoding
    • Configure caching
    • Monitor support
    • Handle errors
  4. Maintenance:

    • Monitor usage
    • Update resources
    • Regular testing
    • Document changes
    • Review performance

Built with excellence by Hellenic Development, delivering enterprise-grade solutions.