I'd like the static middleware to add a Server-Timing header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing

This would allow me to better understand whether an asset is being served slowly because of some issue on the machine or because of network

I tried to implement myself with a middleware but found out it's not really possible because it would require me to write the Server-Timing header after Next is called which isn't well supported

Comment From: joaohaas

Hi, kind of necroing, but you can do this by hijacking the context's writer:

type afterMiddlewareWriter struct {
    gin.ResponseWriter
}

func (w *afterMiddlewareWriter) WriteHeader(statusCode int) {
    // You can freely add headers here using the structs fields
    w.Header().Add("My-Header", "My Value")
    w.ResponseWriter.WriteHeader(statusCode)
}

func AfterMiddleware(c *gin.Context) {
    c.Writer = &afterMiddlewareWriter{c.Writer}
    c.Next()
}

func main() {
    r := gin.Default()
    r.Use(AfterMiddleware)
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "key": "value",
        })
    })
}