Apply gzip compression to dynamic pages.

It should be beneficial to reduce bandwidth and speed up page load time.
Many dynamic pages will compress well (e.g., from 521 KB to 27.2 KB).
This is in large part because inline SVG are often repeated many times.

Only do so if the handler hasn't already taken care of compression.
This is determined by whether the Content-Encoding header is set
by the time WriteHeader is called.
dmitshur committed 4 years ago commit 58d6128437a163a2dd5351a1b77fbe5b892e6398
@@ -72,17 +72,17 @@ func run(ctx context.Context, router Router, analyticsFile string) error {
		if err != nil {
			return err

	server := &http.Server{Addr: *httpFlag, Handler: top{&errorHandler{handler: (&handler{
	server := &http.Server{Addr: *httpFlag, Handler: top{gzipHandler{&errorHandler{handler: (&handler{
		rtr:           router,
		analyticsHTML: analyticsHTML,
		fontsHandler:  httpgzip.FileServer(assets.Fonts, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed}),
		assetsHandler: httpgzip.FileServer(assets.Assets, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed}),
		s:             newService(ctx),

	go func() {
		err := server.Close()
		if err != nil {
@@ -1,19 +1,22 @@
package main

import (


// errorHandler factors error handling out of the HTTP handler.
type errorHandler struct {
	handler func(w http.ResponseWriter, req *http.Request) error
@@ -105,10 +108,80 @@ func (rw *headerResponseWriter) Write(p []byte) (n int, err error) {
func (rw *headerResponseWriter) WriteHeader(code int) {
	rw.WroteHeader = true

// gzipHandler applies gzip compression on top of Handler, unless Handler
// has already handled it (i.e., the "Content-Encoding" header is set).
type gzipHandler struct {
	Handler http.Handler

func (h gzipHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	// If request doesn't accept gzip encoding, serve without compression.
	if !httpguts.HeaderValuesContainsToken(req.Header["Accept-Encoding"], "gzip") {
		h.Handler.ServeHTTP(w, req)

	// Otherwise, use gzipResponseWriter to start gzip compression when WriteHeader
	// is called, but only if the handler didn't already take care of it.
	rw := &gzipResponseWriter{ResponseWriter: w}
	defer rw.Close()
	h.Handler.ServeHTTP(rw, req)

// gzipResponseWriter starts gzip compression when WriteHeader is called, unless compression
// has already been applied by that time (i.e., the "Content-Encoding" header is set).
type gzipResponseWriter struct {

	// These fields are set by setWriterAndCloser
	// during first call to Write or WriteHeader.
	w io.Writer // When set, must be non-nil.
	c io.Closer // May be nil.

func (rw *gzipResponseWriter) WriteHeader(code int) {
	if rw.w != nil {
		panic(fmt.Errorf("internal error: gzipResponseWriter: WriteHeader called twice or after Write"))
func (rw *gzipResponseWriter) Write(p []byte) (n int, err error) {
	if rw.w == nil {
	return rw.w.Write(p)

func (rw *gzipResponseWriter) setWriterAndCloser() {
	if _, ok := rw.Header()["Content-Encoding"]; ok {
		// Compression already handled by the handler.
		rw.w = rw.ResponseWriter

	// Update headers, start using a gzip writer.
	rw.Header().Set("Content-Encoding", "gzip")
	gw := gzip.NewWriter(rw.ResponseWriter)
	rw.w = gw
	rw.c = gw

func (rw *gzipResponseWriter) Close() {
	if rw.c == nil {
	err := rw.c.Close()
	if err != nil {
		log.Printf("gzipResponseWriter.Close: error closing *gzip.Writer: %v", err)

// top adds some instrumentation on top of Handler.
type top struct{ Handler http.Handler }

func (t top) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	path := req.URL.Path