Skip to content

Commit b670fad

Browse files
Merge pull request #154 from ziadoz/skip-vite-hot
Skip CSP middleware when Vite is hot reloading
2 parents e42076a + e441c08 commit b670fad

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/AddCspHeaders.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Vite;
78
use Symfony\Component\HttpFoundation\Response;
89

910
class AddCspHeaders
@@ -19,8 +20,8 @@ public function handle(
1920
return $response;
2021
}
2122

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

tests/AddCspHeadersTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
use Illuminate\Contracts\Http\Kernel;
4+
use Illuminate\Foundation\Vite;
45
use Illuminate\Support\Facades\Route;
6+
use Mockery\MockInterface;
57
use function PHPUnit\Framework\assertEquals;
68
use function PHPUnit\Framework\assertFalse;
79
use function PHPUnit\Framework\assertNull;
@@ -331,6 +333,22 @@ public function configure(Policy $policy): void
331333
assertFalse($headers->has('content-security-policy'));
332334
});
333335

336+
test('route middleware is skipped when vite is hot reloading', function (): void {
337+
config(['app.debug' => true]);
338+
339+
$this->mock(Vite::class, function (MockInterface $mock): void {
340+
$mock->shouldReceive('isRunningHot')->andReturn(true);
341+
});
342+
343+
Route::get('other-route', function () {
344+
return 'ok';
345+
})->middleware(AddCspHeaders::class.':'.Basic::class);
346+
347+
$headers = getResponseHeaders('other-route');
348+
349+
assertFalse($headers->has('content-security-policy'));
350+
});
351+
334352
it('will handle scheme values', function (): void {
335353
$policy = new class implements Preset {
336354
public function configure(Policy $policy): void

0 commit comments

Comments
 (0)