Home  >  Article  >  Web Front-end  >  About HTTP/2 server push

About HTTP/2 server push

php中世界最好的语言
php中世界最好的语言Original
2017-12-30 18:00:232226browse

Before talking about HTTP/2 server push, we must first know that HTTP/2 is designed to solve many shortcomings of HTTP/1.x. Contemporary web pages use many resources: HTML, style sheets, scripts, images and so on. In HTTP/1.x each of these resources must be requested explicitly. This can be a slow process. So the browser starts by getting the HTML, and then incrementally gets more resources as it parses and evaluates the page. Because the server must wait for the browser to make every request, the network is often idle and underused.

To improve latency, HTTP/2 introduced server push, which allows the server to push resources to the browser before the browser explicitly requests them. A server often knows that a page requires many additional resources and can start pushing those resources when it responds to the browser's first request. This allows the server to fully utilize a potentially idle network, improving page load times.

serverpush.svg.png

At the protocol layer, HTTP/2 server push is driven by push_promise frames. A PUSH_PROMISE describes a request, that is, server-side predictive browsing The server will issue the request immediately. As soon as the browser receives the PUSH_PROMISE, it immediately knows that the server is going to transfer this resource. If the browser subsequently discovers that it needs the resource, it will wait for the push to complete rather than sending a new request. This reduces the time the browser spends waiting for the network.

Server push in net/http package

go1.8 introduces support for push responses from http.Server. This feature is available if the running server is an HTTP/2 service and incoming connections use HTTP/2. In any HTTP handler, you can determine whether http.ResponseWriter supports server push by checking whether it implements the new http.Pusher interface.

For example, if the server knows that app.js will be requested to render the page, the handler can initiate 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)
            }
        }
        // ...
    })

I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to the php Chinese website Other related articles!

Related reading:

What does the JS engine look like when running

How does native JS implement AJAX and JSONP

How to customize the console object during the use of JS

The above is the detailed content of About HTTP/2 server push. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn