Sorry, this entry is only available in Brazilian Portuguese. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

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

In order to take advantage of HTTP2 features there several requirements that must be met:

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

The first thing you need to do in a PHP script to use this package is to retrieve an instance of the class:

1

$pusher=\Melbahja\Http2\Pusher::getInstance();

Then you can add a CSS file using the link method:

1

$pusher->link('/assets/css/style.css');

You can also add JavaScript files using the src method:

1

$pusher->src('/assets/js/myjs.js');

Images can be added using the img method:

1

$pusher->img('/images/logo.png');

You can add multiple resources using the method chaining style:

1

2

3

4

$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.

$pusher->push();

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.