Skip to content

Add configuration for Vite hot reloading #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ return [
*/
'enabled' => env('CSP_ENABLED', true),

/**
* Headers will be added when Vite is hot reloading.
*/
'enabled_while_hot_reloading' => env('CSP_ENABLED_WHILE_HOT_RELOADING', false),

/*
* The class responsible for generating the nonces used in inline tags and headers.
*/
Expand Down
5 changes: 5 additions & 0 deletions config/csp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
*/
'enabled' => env('CSP_ENABLED', true),

/**
* Headers will be added when Vite is hot reloading.
*/
'enabled_while_hot_reloading' => env('CSP_ENABLED_WHILE_HOT_RELOADING', false),

/*
* The class responsible for generating the nonces used in inline tags and headers.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/AddCspHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ public function handle(
return $response;
}

// Skip CSP middleware when Laravel is rendering an exception or Vite is hot reloading
if (config('app.debug') && ($response->isServerError() || Vite::isRunningHot())) {
// Skip CSP middleware when Laravel is rendering an exception
if (config('app.debug') && $response->isServerError()) {
return $response;
}

// Skip CSP middleware when Vite is hot reloading
if (config('app.debug') && ! config('csp.enabled_while_hot_reloading') && Vite::isRunningHot()) {
return $response;
}

Expand Down
1 change: 1 addition & 0 deletions tests/AddCspHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public function configure(Policy $policy): void

test('route middleware is skipped when vite is hot reloading', function (): void {
config(['app.debug' => true]);
config(['csp.enabled_while_hot_reloading' => false]);

$this->mock(Vite::class, function (MockInterface $mock): void {
$mock->shouldReceive('isRunningHot')->andReturn(true);
Expand Down