MCPcopy
hub / github.com/tdewolff/minify / MiddlewareWithError

Method MiddlewareWithError

minify.go:376–385  ·  view source on GitHub ↗

MiddlewareWithError provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter. The error function allows handling minification errors. http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ... Minification might be slower t

(next http.Handler, errorFunc func(w http.ResponseWriter, r *http.Request, err error))

Source from the content-addressed store, hash-verified

374// http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ...
375// Minification might be slower than just sending the original file! Caching is advised.
376func (m *M) MiddlewareWithError(next http.Handler, errorFunc func(w http.ResponseWriter, r *http.Request, err error)) http.Handler {
377 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
378 mw := m.ResponseWriter(w, r)
379 next.ServeHTTP(mw, r)
380 if err := mw.Close(); err != nil {
381 errorFunc(w, r, err)
382 return
383 }
384 })
385}

Callers

nothing calls this directly

Calls 2

ResponseWriterMethod · 0.95
CloseMethod · 0.45

Tested by

no test coverage detected