0

As I understand HTTP/2 can send the website assets (images, scripts, css files, etc) over a single connection, as well as push them. I am interested in how this actually happens in practice.

How does a general-purpose server (such as Apache or nginx) decide what to send and what? In particular, I understand that in theory the server can push data that it knows will be requested, but how does it know what to send?

For example, let's say you have a webserver (Apache or nginx) configured with an index.php script which generates a page and a number of assets. Does the webserver automatically parse the output of index.php and send all the requisite files along with the response? Or does the index.php file need to specify this somehow?

Or is it the case that while possible in theory, this doesn't happen in practice, and the browser just makes a request for the assets later?

  • 2
    http2-explained.haxx.se/en/part6#6-7-server-push - rather than pre-parsing the html output it becomes a known pattern that requests for A are usually followed by requests for (cacheable resources) B and C so the web server can start sending them without being asked when the client wants to support that (and the client can later terminate those downloads when it doesn’t want them ) – HermanB Oct 18 at 20:40
2

This is entirely up the server and how it has been configured.

Most servers are not intelligent enough to know what to push and depend on being configured. So you can set up config to say if any index.html file is requested then push common.css and common.js. It’s important then to consider the next page that’s visited - there is no need to push those files again, as user should already have them. You can use a cookie-based approach to track this. See my post here for how to configure this in Apache. Some servers (e.g. Apache) also maintain a list of known pushed assets for that connection to avoid over pushing, though that only works on same connection so cookie-based approach is better.

Many servers and CDNs can use HTTP link headers to inform the web server which assets to push. This allows the control to rest with backend application servers, but the push to happen from the edge web servers so it doesn’t all need to be explicitly configured on the web server.

Other servers try to be more intelligent with this and try to guess what resources to push based on observing requests. Jetty has a facility to do this for example. How accurate or useful this is I can’t attest to.

Or is it the case that while possible in theory, this doesn't happen in practice, and the browser just makes a request for the assets later?

While it is certainly possible to push (my blog does it as can be seen in above post), there are real problems with push. Over-pushing is a real risk and even without that the benefits have never really been proven. Plus there are implementation issues and complications to consider too. So the reality is it is not used much. From a study I completed last year about half a percent (0.5%) of sites used HTTP/2 push. Chrome has stated for a while now that it is considering turning off support for HTTP/2 push. Use it with caution.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.