Skip to content

Commit 9619d76

Browse files
authored
Add support for Vite CSP nonce
Boost Browserlogger won't work when CSP is enabled with a Vite provided nonce. This commit adds the Vite nonce when its detected. Boost is meant for local development and CSP is often applied in production only. Yet for testing purposes CSP can be applied in the local environment as well, making it useful for Boost to support CSP nonces as well.
1 parent 384ffc5 commit 9619d76

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Services/BrowserLogger.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Laravel\Boost\Services;
66

77
use Illuminate\Support\Facades\Route;
8+
use Illuminate\Support\Facades\Vite;
89

910
class BrowserLogger
1011
{
@@ -14,8 +15,18 @@ public static function getScript(): string
1415
? route('boost.browser-logs')
1516
: '/_boost/browser-logs';
1617

18+
$attributes = ['id' => 'browser-logger-active'];
19+
20+
if ($nonce = Vite::cspNonce()) {
21+
$attributes['nonce'] = $nonce;
22+
}
23+
24+
$scriptAttributes = collect($attributes)
25+
->map(fn ($value, $key) => sprintf('%s="%s"', $key, $value))
26+
->implode(' ');
27+
1728
return <<<HTML
18-
<script id="browser-logger-active">
29+
<script {$scriptAttributes}>
1930
(function() {
2031
const ENDPOINT = '{$endpoint}';
2132
const logQueue = [];

0 commit comments

Comments
 (0)