This is a creation in Article, where the information may have evolved or changed.
Translation: shihuaping0918@163.com
"Translator Note: This article needs to have certain HTTP protocol knowledge and browser parsing page process knowledge"
Original: Https://blog.golang.org/h2push
Objective
HTTP/2 is designed to solve many of http/1.x's flaws. Contemporary Web pages use a lot of resources: HTML, stylesheets, scripts, pictures, and so on. Each of these resources must be explicitly requested in the http/1.x. This can be a slow process. The browser starts with getting HTML, and then incrementally gets more resources when it parses and evaluates the page. Because the server must wait for the browser to make every request, the network is often idle and underutilized.
To improve latency, HTTP/2 introduced the server push, which allows servers to push resources to the browser before the browser explicitly requests it. A server often knows that a page requires a lot of additional resources, and when it responds to the browser's first request, it can start pushing these resources. This allows the server to fully utilize a potentially idle network to improve page load times.
Serverpush.svg.png
At the protocol layer, HTTP/2 server push is driven by push_promise frames, and a push_promise describes a request that the server expects the browser to make a request immediately. Once the browser receives the push_promise, it immediately knows that the server will be transmitting this resource. If the browser later discovers that it needs this resource, it waits for the push to complete instead of sending a new request. This reduces the amount of time the browser spends waiting on the network.
Service-side push in the Net/http package
go1.8 introduces support for push responses from Http.server. This feature is available if the server that is running is a HTTP/2 service and the incoming connection is using HTTP/2. In any HTTP handler, you can judge http. Whether the Responsewriter supports server-side push, by checking whether it implements the new HTTP. Pusher interface.
For example, if the server knows that App.js is going to be requested to render the page, handler can initialize a push if Http.pusher is available.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if pusher, ok := w.(http.Pusher); ok { // Push is supported. if err := pusher.Push("/app.js", nil); err != nil { log.Printf("Failed to push: %v", err) } } // ... })
Because of the time relationship, this evening first translated to this ...