Git Product home page Git Product logo

push's Introduction

Push Build Status GoDoc Coverage Status

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

  • url(data:image/svg+xml,...) as SVG data URI SVGs are not allowed to load external resources

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.

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.