justbetter/laravel-http3earlyhints

A HTTP3 Early Hints Middleware for Laravel

1.0.4 2024-05-29 14:33 UTC

This package is auto-updated.

Last update: 2024-05-29 14:35:15 UTC


README

Early Hints is a HTTP/3 concept which allows the server to send preconnect and preload headers while it's still preparing a response. This allows the broser to start loading these resources before the server has finished building and sending a response See.

This package aims to provide the easiest experience for adding Early Hints to your responses. Simply route your requests through the AddHttp3EarlyHints middleware and it will automatically create and attach the Link headers necessary to implement Early Hints for your CSS, JS and Image assets.

Installation

You can install the package via composer:

$ composer require justbetter/laravel-http3earlyhints

Next you must add the \JustBetter\Http3EarlyHints\Middleware\AddHttp3EarlyHints-middleware to the kernel. Adding it to the web group is recommeneded as API's do not have assets to push.

// app/Http/Kernel.php

...
protected $middlewareGroups = [
    'web' => [
        ...
        \JustBetter\Http3EarlyHints\Middleware\AddHttp3EarlyHints::class,
        ...
    ],
    ...
];

Publish config

php artisan vendor:publish --provider="JustBetter\Http3EarlyHints\ServiceProvider"

Note: send_103 defaults to false, this is because it isn't actually supported widely yet. Currently only FrankenPHP supports Early Hints natively. default behaviour is adding the link headers to the 200 response which e.g. Cloudflare turns into early hints.

Usage

When you route a request through the AddHttp3EarlyHints middleware, the response is scanned for any link, script or img tags that could benefit from being loaded using Early Hints. These assets will be added to the Link header before sending the response to the client. Easy!

Note: To push an image asset, it must have one of the following extensions: bmp, gif, jpg, jpeg, png, tiff or svg and not have loading="lazy"

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.