Skip to content

Commit

Permalink
Automatically add crossorigin for modulepreload
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning committed Nov 6, 2024
1 parent af2bd17 commit d176275
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Listeners/AddFromBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function handle(GenerateEarlyHints $event)
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $referrerpolicy, $imagesizes, $imagesrcset] = $element;
$rel = $type === 'module' ? 'modulepreload' : $rel;

if ($rel === 'modulepreload' && ! $crossorigin) {
// On module or modulepreload the crossorigin is REQUIRED https://github.com/whatwg/html/issues/1888
$crossorigin = 'anonymous';
}

$attributes = array_filter(@compact('crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'));

return [
Expand Down Expand Up @@ -119,7 +124,7 @@ private function buildLinkHeader(string $url, ?string $rel = 'preload', ?array $
$link = $link->withAttribute('as', $type ?? 'fetch');
}
if ($type === 'font' && empty($attributes['crossorigin'])) {
$link = $link->withAttribute('crossorigin', true);
$link = $link->withAttribute('crossorigin', 'anonymous');
}

return $link;
Expand Down

0 comments on commit d176275

Please sign in to comment.