Push
Push is a package that uses HTTP2 to push resources to the client as it parses content. By parsing HTML, CSS and SVG it extracts referenced resource URIs and pushes them towards the client, which is quicker than waiting for the client to parse and request those resources.
Installation
You need Go1.8 (from tip for example).
Run the following command
go get github.com/tdewolff/push
or add the following import and run the project with go get
import (
"github.com/tdewolff/push"
)
Parsers
HTML
Parses
<style>...</style>
as CSS<x style="...">
as inline CSS<iframe>...</iframe>
as HTML<svg>...</svg>
as SVG
Extracts URIs from
<link href="...">
<script src="...">
<img src="...">
<img srcset="..., ...">
<object data="...">
<source src="...">
<audio src="...">
<video src="...">
<track src="...">
<embed src="...">
<input src="...">
<iframe src="...">
CSS
Parses
data URI SVGs are not allowed to load external resourcesurl(data:image/svg+xml,...)
as SVG
Extracts URIs from
url("...")
SVG
Parses
<style>...</style>
as CSS<x style="...">
as inline CSS
Extracts URIs from
<script href="..." xlink:href="...">
<image href="..." xlink:href="...">
<feImage href="..." xlink:href="...">
<color-profile href="..." xlink:href="...">
<use href="..." xlink:href="...">
Usage
Middleware
fileOpener := push.DefaultFileOpener("resources/")
cache := push.DefaultCache()
p := push.New("example.com/", fileOpener, cache)
http.HandleFunc("/", p.Middleware(func(w http.ResponseWriter, r *http.Request) {
// ...
}))
Pass nil
for fileOpener
and cache
to disable recursive parsing and URI caching respectively.
ResponseWriter
Wrap an existing http.ResponseWriter
so that it pushes resources automatically:
fileOpener := push.DefaultFileOpener("resources/")
cache := push.DefaultCache()
p := push.New("example.com/", fileOpener, cache)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if pushWriter, err := p.ResponseWriter(w, r); err == nil {
defer pushWriter.Close() // Close returns an error...
w = pushWriter
}
// ...
})
Pass nil
for fileOpener
and cache
to disable recursive parsing and URI caching respectively.
Reader
Wrap a reader and obtain the URIs from a channel:
func openIndex() io.Reader {
r, _ := os.Open("index.html")
uriHandler := push.URIHandlerFunc(func(uri string) error {
// is called concurrently when using a recursive parser
return nil
})
fileOpener := push.FileOpenerFunc(func(uri string) (io.Reader, string, error) {
// open file for uri
return r, mimetype, nil
})
parser := push.NewParser("example.com/", fileOpener, uriHandler)
return p.Reader(r, parser, "text/html", "/index.html")
}
List
List the resource URIs found:
r, _ := os.Open("index.html")
uris, err := push.List("example.com/", fileOpener, r, "text/html", "/index.html")
if err != nil {
panic(err)
}
Low-level usage
Push
pushes resources to pusher
. It is the underlying functionality of ResponseWriter
.
httpPusher, ok := w.(http.Pusher)
if ok {
pusher := NewPusher(httpPusher, &http.PushOptions{"", http.Header{}})
parser, err := push.NewParser("example.com/", nil, pusher)
if err != nil {
panic(err)
}
tr := io.TeeReader(r, w)
err := parser.Parse(r, "text/html", "/index.html")
}
Example
See example, it shows how a webserver with artificial 50ms delay per request can have the page load time reduced from 1.6s (http) to 0.4s (https).
License
Released under the MIT license.