Skip to content

Commit af2bd17

Browse files
committed
Pass more information from the link headers
1 parent f3fc8ce commit af2bd17

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Data/LinkHeaders.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function makeUnique()
101101
$handledHashes = [];
102102

103103
foreach ($this->getLinkProvider()->getLinks() as $link) {
104-
$hash = md5(serialize($link));
104+
/** @var Link $link */
105+
$hash = md5($link->getHref(), serialize($link->getRels()));
105106
if (! in_array($hash, $handledHashes)) {
106107
$handledHashes[] = $hash;
107108

src/Listeners/AddFromBody.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ public function handle(GenerateEarlyHints $event)
1919
$excludeKeywords = array_filter(config('http3earlyhints.exclude_keywords', []));
2020
$headers = $this->fetchLinkableNodes($event->response)
2121
->flatMap(function ($element) {
22-
[$src, $href, $data, $rel, $type] = $element;
22+
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $referrerpolicy, $imagesizes, $imagesrcset] = $element;
2323
$rel = $type === 'module' ? 'modulepreload' : $rel;
2424

25+
$attributes = array_filter(@compact('crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'));
26+
2527
return [
26-
$this->buildLinkHeader($src ?? '', $rel ?? null),
27-
$this->buildLinkHeader($href ?? '', $rel ?? null),
28-
$this->buildLinkHeader($data ?? '', $rel ?? null),
28+
$this->buildLinkHeader($href ?? '', $rel ?? null, $attributes),
29+
$this->buildLinkHeader($src ?? '', $rel ?? null, $attributes),
30+
$this->buildLinkHeader($data ?? '', $rel ?? null, $attributes),
2931
];
3032
})
3133
->filter(function (?Link $value) use ($excludeKeywords) {
@@ -60,13 +62,16 @@ protected function fetchLinkableNodes(Response $response): Collection
6062
{
6163
$crawler = $this->getCrawler($response);
6264

63-
return collect($crawler->filter('link:not([rel*="icon"]):not([rel="canonical"]):not([rel="manifest"]):not([rel="alternate"]), script[src]:not([defer]):not([async]), *:not(picture)>img[src]:not([loading="lazy"]), object[data]')->extract(['src', 'href', 'data', 'rel', 'type']));
65+
return collect(
66+
$crawler->filter('link:not([rel*="icon"]):not([rel="canonical"]):not([rel="manifest"]):not([rel="alternate"]), script[src]:not([defer]):not([async]), *:not(picture)>img[src]:not([loading="lazy"]), object[data]')
67+
->extract(['src', 'href', 'data', 'rel', 'type', 'crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'])
68+
);
6469
}
6570

6671
/**
6772
* Build out header string based on asset extension.
6873
*/
69-
private function buildLinkHeader(string $url, ?string $rel = 'preload'): ?Link
74+
private function buildLinkHeader(string $url, ?string $rel = 'preload', ?array $attributes = []): ?Link
7075
{
7176
$linkTypeMap = [
7277
'.CSS' => 'style',
@@ -102,12 +107,18 @@ private function buildLinkHeader(string $url, ?string $rel = 'preload'): ?Link
102107

103108
$link = new Link($rel, $url);
104109

110+
foreach ($attributes as $key => $value) {
111+
$link = $link->withAttribute($key, $value);
112+
}
113+
105114
if ($rel === 'preconnect' && $url) {
106115
return $link;
107116
}
108117

109-
$link = $link->withAttribute('as', $type ?? 'fetch');
110-
if ($type === 'font') {
118+
if (empty($attributes['as'])) {
119+
$link = $link->withAttribute('as', $type ?? 'fetch');
120+
}
121+
if ($type === 'font' && empty($attributes['crossorigin'])) {
111122
$link = $link->withAttribute('crossorigin', true);
112123
}
113124

0 commit comments

Comments
 (0)