Enabling brotli compression in Go and Chi

Why Brotli?

Brotli is supported on most modern browsers. For browsers that don’t support Brotli, the compression method will fall back to gzip or deflate based on the request’s Accept-Encoding header. Enabling Brotli compression is an easy p…


This content originally appeared on DEV Community and was authored by John Pham

Why Brotli?

Brotli is supported on most modern browsers. For browsers that don't support Brotli, the compression method will fall back to gzip or deflate based on the request's Accept-Encoding header. Enabling Brotli compression is an easy performance win.

Brotli is usually more effective at compressing compared to gzip and deflate. When I enabled Brotli compression for Highlight, I saw response sizes decrease around 40% with no latency increases (for the most part requests were faster!).

The Code Changes

Installing Dependencies

# Install the Chi, older versions of Chi don't support Brotli
go get -u github.com/go-chi/chi

# Install the package that will do the Brotli compression
go get -u gopkg.in/kothar/brotli-go.v0

Enabling Brotli Compression

r := chi.NewMux()
// /* means to compress all content types that can be compressed.
compressor := middleware.NewCompressor(5, "/*")
compressor.SetEncoder("br", func(w io.Writer, level int) io.Writer {
    params := brotli_enc.NewBrotliParams()
    params.SetQuality(level)
    return brotli_enc.NewBrotliWriter(params, w)
})
r.Use(compressor.Handler)

The "/*" means to compress all content types that can be compressed. These are the supported types:

var defaultCompressibleContentTypes = []string{
    "text/html",
    "text/css",
    "text/plain",
    "text/javascript",
    "application/javascript",
    "application/x-javascript",
    "application/json",
    "application/atom+xml",
    "application/rss+xml",
    "image/svg+xml",
}

That's it, congrats on the easy performance win for you and your users!


This content originally appeared on DEV Community and was authored by John Pham


Print Share Comment Cite Upload Translate Updates
APA

John Pham | Sciencx (2021-09-07T23:17:48+00:00) Enabling brotli compression in Go and Chi. Retrieved from https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/

MLA
" » Enabling brotli compression in Go and Chi." John Pham | Sciencx - Tuesday September 7, 2021, https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/
HARVARD
John Pham | Sciencx Tuesday September 7, 2021 » Enabling brotli compression in Go and Chi., viewed ,<https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/>
VANCOUVER
John Pham | Sciencx - » Enabling brotli compression in Go and Chi. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/
CHICAGO
" » Enabling brotli compression in Go and Chi." John Pham | Sciencx - Accessed . https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/
IEEE
" » Enabling brotli compression in Go and Chi." John Pham | Sciencx [Online]. Available: https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/. [Accessed: ]
rf:citation
» Enabling brotli compression in Go and Chi | John Pham | Sciencx | https://www.scien.cx/2021/09/07/enabling-brotli-compression-in-go-and-chi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.