Skip to content

Commit 944a3ad

Browse files
committed
Remove imagesizes and imagesrcset, add nonce
1 parent 375270b commit 944a3ad

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ So we will not preload these images.
9898

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

101+
##### Nonce
102+
103+
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.
104+
105+
Without hardcoding the nonce
106+
[Vite::useCspNonce($nonce);](https://laravel.com/docs/11.x/vite#content-security-policy-csp-nonce:~:text=Vite%3A%3AuseCspNonce(%24nonce)%3B)
107+
sending this in early hints will be useless as each request will send early hints with a stale nonce.
108+
101109
## Testing
102110

103111
``` bash

src/Listeners/AddFromBody.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public function handle(GenerateEarlyHints $event)
2121
$excludeKeywords = array_filter(config('http3earlyhints.exclude_keywords', []));
2222
$headers = $this->fetchLinkableNodes($event->response)
2323
->flatMap(function ($element) {
24-
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $referrerpolicy, $imagesizes, $imagesrcset] = $element;
24+
[$src, $href, $data, $rel, $type, $crossorigin, $as, $fetchpriority, $integrity, $nonce, $referrerpolicy] = $element;
2525
$rel = $type === 'module' ? 'modulepreload' : $rel;
2626

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

32-
$attributes = array_filter(@compact('crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'));
32+
$attributes = array_filter(@compact('crossorigin', 'as', 'fetchpriority', 'integrity', 'nonce', 'referrerpolicy'));
3333

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

7272
return collect(
7373
$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]')
74-
->extract(['src', 'href', 'data', 'rel', 'type', 'crossorigin', 'as', 'fetchpriority', 'integrity', 'referrerpolicy', 'imagesizes', 'imagesrcset'])
74+
->extract(['src', 'href', 'data', 'rel', 'type', 'crossorigin', 'as', 'fetchpriority', 'integrity', 'nonce', 'referrerpolicy'])
7575
);
7676
}
7777

0 commit comments

Comments
 (0)