Skip to content

Commit

Permalink
Remove imagesizes and imagesrcset, add nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning committed Jan 27, 2025
1 parent 375270b commit 944a3ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ So we will not preload these images.

If your html object tag contains `data=""` it will preload it.

##### Nonce

While the early hints module does support sending [nonce](https://laravel.com/docs/11.x/vite#content-security-policy-csp-nonce) across as well, we recommend against it. And use [integrity](https://laravel.com/docs/11.x/vite#subresource-integrity-sri) instead.

Without hardcoding the nonce
[Vite::useCspNonce($nonce);](https://laravel.com/docs/11.x/vite#content-security-policy-csp-nonce:~:text=Vite%3A%3AuseCspNonce(%24nonce)%3B)
sending this in early hints will be useless as each request will send early hints with a stale nonce.

## Testing

``` bash
Expand Down
6 changes: 3 additions & 3 deletions src/Listeners/AddFromBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function handle(GenerateEarlyHints $event)
$excludeKeywords = array_filter(config('http3earlyhints.exclude_keywords', []));
$headers = $this->fetchLinkableNodes($event->response)
->flatMap(function ($element) {
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $referrerpolicy, $imagesizes, $imagesrcset] = $element;
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $nonce, $referrerpolicy] = $element;
$rel = $type === 'module' ? 'modulepreload' : $rel;

if ($rel === 'modulepreload' && empty($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'));
$attributes = array_filter(@compact('crossorigin', 'as', 'fetchpriority', 'integrity', 'nonce', 'referrerpolicy'));

return [
$this->buildLinkHeader($href ?? '', $rel ?? null, $attributes),
Expand Down Expand Up @@ -71,7 +71,7 @@ protected function fetchLinkableNodes(Response $response): Collection

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', 'crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'])
->extract(['src', 'href', 'data', 'rel', 'type', 'crossorigin', 'as', 'fetchpriority', 'integrity', 'nonce', 'referrerpolicy'])
);
}

Expand Down

0 comments on commit 944a3ad

Please sign in to comment.