Skip to content

Commit d630c8a

Browse files
committed
Check the format to determine if link headers should be added, allow disabling on-the-fly generation
1 parent 7355e4c commit d630c8a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Middleware/AddHttp3EarlyHints.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function handle(Request $request, Closure $next, ?int $sizeLimit = null):
2525
{
2626
$lastPath = Str::afterLast($request->path(), '/');
2727
if (
28-
$request->isJson()
28+
$request->format() !== 'html'
2929
|| (str_contains($lastPath, '.') && ! in_array(Str::afterLast($lastPath, '.'), config('http3earlyhints.extensions', ['', 'php', 'html'])))
3030
) {
3131
$this->skipCurrentRequest = true;
@@ -38,6 +38,9 @@ public function handle(Request $request, Closure $next, ?int $sizeLimit = null):
3838
$linkHeaders = Cache::store(config('http3earlyhints.cache_driver'))->get('earlyhints-'.md5($request->url()));
3939
if (! $linkHeaders) {
4040
$response = $next($request);
41+
if (! config('http3earlyhints.generate_during_request', true)) {
42+
return $response;
43+
}
4144
$linkHeaders = $this->handleGeneratingLinkHeaders($request, $response);
4245
if ($linkHeaders) {
4346
$this->addLinkHeaders($response, $linkHeaders);

src/config.php

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
'cache_duration' => env('EARLY_HINTS_CACHE_DURATION', 864000 /* 24h */),
99
'cache_driver' => env('EARLY_HINTS_CACHE_DRIVER', null),
10+
/**
11+
* We always crawl the response after the response has been sent.
12+
* If we do not have it in cache, should we crawl during the request to always send link headers?
13+
*/
14+
'generate_during_request' => env('EARLY_HINTS_GENERATE_DURING_REQUEST', true),
1015
/** Wether to send the 103. This means we do not need an external party to handle early hints but is currently only supported by FrankenPHP */
1116
'send_103' => env('EARLY_HINTS_SEND_103', false) && \function_exists('headers_send'),
1217
/** Size limit in bytes */

0 commit comments

Comments
 (0)