Author: Mohamed Elbahja
Posted on: 2017-10-25
Package: HTTP2 Server Push PHP Class
Read this article to learn how to take advantage of the HTTP2 Server Push feature to improve the performance of PHP sites.
Contents
HTTP2 Performance Advantages
HTTP2 Requirements
How to Install the HTTP2 Server Push PHP Class
How to Use the HTTP2 Server Push PHP Class
Contributing and Support
HTTP2 Performance Advantages
HTTP/2 is a newer version of the HTTP protocol that provides several enhancements for achieving better data access performance like for example using only one TCP connection, as well the server push feature to push all files needed by a page before they are requested like CSS, JavaScript, images etc..
The server push features works by sending special headers to the browser to let it know what are the resources the page needs to be loaded and rendered. Then the browser will load those CSS, JavaScript and Image files in parallel while the current page is still being loaded.
In simple terms, since the HTTP/2 server push can make the server return all the page resources using a single network connection, this feature will make your Web site faster.
In this article I will show you how to use the HTTP2 Server Push PHP Class package to take advantage of the HTTP/2 server push feature.
HTTP2 Requirements
1. Server support for HTTP/2
2. Browser support for HTTP/2
3. HTTPS connections using TLS 1.2: although the specification of HTTP/2 does not require HTTPS support, most browsers require HTTPS to support HTTP/2
If any of these conditions are not met, the requests will fallback to HTTP/1.1.
How to Install the HTTP2 Server Push PHP Class
The HTTP2 Server Push PHP Class can be installed using the Composer tool following instructions in the download page.
Alternatively you can also download the package ZIP archive from the same download page and manually include the class file in your application scripts.
How to Use the HTTP2 Server Push PHP Class
$pusher = \Melbahja\Http2\Pusher::getInstance();
Then you can add a CSS file using the link method:
$pusher->link('/assets/css/style.css');
You can also add JavaScript files using the src method:
$pusher->src('/assets/js/myjs.js');
Images can be added using the img method:
$pusher->img('/images/logo.png');
You can add multiple resources using the method chaining style:
$pusher->img('/uploads/post.jpg')
->link('/assets/css/bootstrap.css')
->js('/assets/js/jquery.js')
->set(Pusher::LINK, '/assets/css/main.css');
After adding all resources you want to push use the method push to make the class send Link headers to the current request response. This will signal the browser to preload all the resources for the current page in parallel, thus making the whole page load faster.
Contributing and Support
If you liked this package and would to contribute to its development, you can send your pull requests to the package repository in Github.
if you have any questions or remarks about the package post a comment here below.
You need to be a registered user or login to post a comment
1,470,254 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
2. Browser Cache? - Dave D (2017-10-25 22:45)
Does the class override browser caching?... - 2 replies
Read the whole comment and replies
1. HTTP requests? - Robert Ames (2017-10-25 14:34)
Reducing the number of HTTP requests... - 1 reply
Read the whole comment and replies