laravel-early-hints maintained by lenorix
Tell to client what will need later
Lets the client know as early as possible what resources it will need, so it can start loading them sooner. This package provides a facade to add Link HTTP headers (e.g. preconnect) to a response.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require lenorix/laravel-early-hints
Usage
Use the EarlyHints facade to add a Link header to a response.
addLinkHeader
Adds a single URI to the Link header.
use Lenorix\LaravelEarlyHints\Facades\EarlyHints;
EarlyHints::addLinkHeader($response, 'https://one.example.com');
EarlyHints::addLinkHeader($response, 'https://fonts.example.com/font.woff2', rel: 'preload', as: 'font', fetchpriority: 'high');
addLinkHeaders
Same as addLinkHeader, but accepts an array of URIs.
use Lenorix\LaravelEarlyHints\Facades\EarlyHints;
EarlyHints::addLinkHeaders($response, [
'https://one.example.com',
'https://two.example.com',
]);
Both methods default rel to preconnect, while as and fetchpriority are omitted from the header unless provided. Any existing Link header on the response is preserved and the new entries are appended to it.
Automatic mode
The early-hints middleware inspects the response and adds the Link header for you. It is opt-in, so add it wherever you want it to run:
Route::get('/dashboard', DashboardController::class)->middleware('early-hints');
It detects, on HTML responses:
- stylesheets (
<link rel="stylesheet">) asrel="preload"; as="style" - scripts (
<script src>) asrel="preload"; as="script" - images, by extension (
bmp,gif,jpg,jpeg,png,svg,tiff,webp,avif) asrel="preload"; as="image"
And on JSON responses:
- images, the same way as in HTML
- pagination links, from the
next_page_url,next(asrel="next") andprev_page_url,previous,prev(asrel="prev") root properties, when they are notnull
URIs are detected in every form: full URLs, protocol relative URLs, absolute paths and relative paths, with or without a signed query string. Streamed responses, downloads and redirects are left untouched.
Detected links are added from the shortest URI to the longest, and stop before the Link header grows past 4KB, which is where web servers usually start rejecting headers.
Each detection can be turned off in the published config file:
php artisan vendor:publish --tag="laravel-early-hints-config"
return [
'styles_and_scripts' => true,
'images' => true,
'json_pagination' => true,
];
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The Unlicense. Please see License File for more information.