Coder Social home page Coder Social logo

7shu / gzipped Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lpar/gzipped

0.0 2.0 0.0 9 KB

Drop-in replacement for golang http.FileServer which supports gzipped static content

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

gzipped's Introduction

GoDoc

gzipped.FileServer

Drop-in replacement for golang http.FileServer which supports static content compressed with gzip (including zopfli) or brotli.

This allows major bandwidth savings for CSS, JavaScript libraries, fonts, and other static compressible web content. It also means you can compress the content without significant runtime penalty.

Example

Suppose /var/www/assets/css contains your style sheets, and you want to make them available as /css/*.css:

package main

import (
	"log"
	"net/http"

	"github.com/lpar/gzipped"
)

func main() {
	log.Fatal(http.ListenAndServe(":8080", http.StripPrefix("/css",
    gzipped.FileServer(http.Dir("/var/www/assets/css")))))
}
// curl localhost:8080/css/styles.css

Using httprouter?

router := httprouter.New()
router.Handler("GET", "/css/*filepath", 
  gzipped.FileServer(http.Dir("/var/www/assets/css"))))
log.Fatal(http.ListenAndServe(":8080", router)

Detail

For any given request at /path/filename.ext, if:

  1. There exists a file named /path/filename.ext.(gz|br) (starting from the appropriate base directory), and
  2. the client will accept content compressed via the appropriate algorithm, and
  3. the file can be opened,

then the compressed file will be served as /path/filename.ext, with a Content-Encoding header set so that the client transparently decompresses it. Otherwise, the request is passed through and handled unchanged.

Unlike other similar code I found, this package has a license, parses Accept-Encoding headers properly, and has unit tests.

Caveats

All requests are passed to Go's standard http.ServeContent method for fulfilment. MIME type sniffing, accept ranges, content negotiation and other tricky details are handled by that method.

It is up to you to ensure that your compressed and uncompressed resources are kept in sync.

Directory browsing isn't supported. (You probably don't want it on your application anyway, and if you do then you probably don't want Go's default implementation.)

Related

  • You might consider precompressing your CSS with minify.

  • If you want to get the best possible compression for clients which don't support brotli, use zopfli.

  • To compress your dynamically-generated HTML pages on the fly, I suggest gziphandler.

gzipped's People

Contributors

lpar avatar eklitzke avatar

Watchers

James Cloos avatar Chen.Tao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.