krenor / http2-pusher
Cache aware HTTP/2 pushing for Laravel.
Requires
- php: >=7.0.0
- illuminate/container: ^5.5
- illuminate/http: ^5.5
- illuminate/routing: ^5.5
- illuminate/support: ^5.5
- symfony/css-selector: ^3.4
- symfony/dom-crawler: ^3.4
Requires (Dev)
- diablomedia/phpunit-pretty-printer: ^2.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-10-22 20:37:44 UTC
README
HTTP/2 is a great advancement for the HTTP protocol, allowing multiple assets to be streamed over a single TCP connection. This reduces the need for “optimisation practices” such as domain sharding, image sprites, etc. There is one really cool feature of HTTP/2 however which can greatly speed up the render time of your website, and that is server push. Server push allows you to send your assets along with the HTML payload before the browser even knows it needs those assets. (Source)
Without cache digests there is no clear-cut performance win for HTTP/2 Server Push over HTTP/1 Asset Bundling. Cache digest is a specification currently under discussion at the IETF HTTP Working Group. (Source)
This package aims to create a cache aware mechanism for HTTP/2 Server Push until cache digest is available. It helps to push exactly what is needed; no more, to waste bandwith and no less, which would result in round trip latency.
Installation
You can install the package via composer:
composer require krenor/http2-pusher
Laravel 5.5 uses Package Auto-Discovery, so it doesn't require you to manually add the ServiceProvider to your providers array configuration.
Configuration
You can configure three things
cookie
name
(default:h2_cache-digest
)duration
Requires a validstrtotime
value (default:60 days
)
global_pushes
Assets you want to be pushed for every page load
Usage
- When you route a request through the
ServerPush
middleware, the response is scanned (unless its aRedirectResponse
or either ajson
orajax
request) for any assets that can be pushed. - Alternatively you can use the
pushes()
method on theResponse
class, which extends the default\Illuminate\Http\Response
, provided by this package's ServiceProvider. - Using the
response()
helper works as fine, however hinting of thepushes()
method will not be available.
Both methods will add a Link
header and a Cookie to the response with all the assets found and the configured via global_pushes
. On subsequent requests the cookie will be scanned with its already pushed resources. If any new resources are available or if the pushed resources have changed the Link
header and the Cookie will be extended to include these.
Note: Only these extensions are currently supported.
This isn't strictly "cache-aware" in the sense that the server knows for sure if the asset is cached on the client side, but the logic follows. If you don't have the luxury of being able to use a web server like H2O or the H2PushDiarySize directive for Apache's mod_http2
module, this solution may work well enough for your purposes.
Contributing
Please see CONTRIBUTING for more information.
Licence
The MIT License. Please see LICENSE for more information.