From 03aacdb8ff3d49fc29b219e21f3426d5eb06a03b Mon Sep 17 00:00:00 2001 From: HolgerHatGarKeineNode <123783602+HolgerHatGarKeineNode@users.noreply.github.com> Date: Wed, 20 May 2026 00:42:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20**Handle=20stale=20compiled=20vi?= =?UTF-8?q?ew=20exceptions=20gracefully**=20-=20=E2=9C=85=20Added=20detect?= =?UTF-8?q?ion=20logic=20to=20identify=20missing=20compiled=20view=20files?= =?UTF-8?q?=20and=20avoid=20500=20errors.=20-=20=F0=9F=9B=A0=EF=B8=8F=20Up?= =?UTF-8?q?dated=20exception=20handling=20to=20return=20503=20with=20`Retr?= =?UTF-8?q?y-After`=20for=20stale=20compiled=20views.=20-=20=F0=9F=94=87?= =?UTF-8?q?=20Prevented=20logging=20of=20stale=20compiled=20view=20excepti?= =?UTF-8?q?ons=20to=20reduce=20noise.=20-=20=E2=9C=85=20Added=20tests=20to?= =?UTF-8?q?=20validate=20503=20responses,=20`Retry-After`=20headers,=20and?= =?UTF-8?q?=20logging=20suppression.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/app.php | 21 +++++++++-- tests/Feature/StaleCompiledViewTest.php | 46 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/StaleCompiledViewTest.php diff --git a/bootstrap/app.php b/bootstrap/app.php index af4aed4..0e9e09d 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -2,6 +2,7 @@ use App\Http\Middleware\DomainMiddleware; use App\Http\Middleware\SetTimezone; +use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; @@ -52,17 +53,33 @@ return Application::configure(basePath: dirname(__DIR__)) return false; }; - $exceptions->report(function (Throwable $e) use ($isStaleLivewireAsset) { + $isStaleCompiledView = function (Throwable $e): bool { + if (! $e instanceof FileNotFoundException) { + return false; + } + + return str_contains($e->getMessage(), '/storage/framework/views/'); + }; + + $exceptions->report(function (Throwable $e) use ($isStaleLivewireAsset, $isStaleCompiledView) { if ($isStaleLivewireAsset($e, request())) { return false; } + + if ($isStaleCompiledView($e)) { + return false; + } }); - $exceptions->render(function (Throwable $e, Request $request) use ($isStaleLivewireAsset) { + $exceptions->render(function (Throwable $e, Request $request) use ($isStaleLivewireAsset, $isStaleCompiledView) { if ($isStaleLivewireAsset($e, $request)) { return response('', 404); } + if ($isStaleCompiledView($e)) { + return response('', 503)->header('Retry-After', '5'); + } + return null; }); })->create(); diff --git a/tests/Feature/StaleCompiledViewTest.php b/tests/Feature/StaleCompiledViewTest.php new file mode 100644 index 0000000..b318d45 --- /dev/null +++ b/tests/Feature/StaleCompiledViewTest.php @@ -0,0 +1,46 @@ +get('/_test/stale-compiled-view'); + + $response->assertStatus(503); + expect($response->headers->get('Retry-After'))->toBe('5'); +}); + +it('does not report missing compiled view exceptions to the logs', function () { + Log::spy(); + + Route::get('/_test/stale-compiled-view-log', function () { + throw new FileNotFoundException( + 'File does not exist at path /var/www/html/storage/framework/views/livewire/views/abc123.blade.php.' + ); + }); + + $this->get('/_test/stale-compiled-view-log')->assertStatus(503); + + Log::shouldNotHaveReceived('error'); + Log::shouldNotHaveReceived('critical'); + Log::shouldNotHaveReceived('emergency'); +}); + +it('still reports FileNotFoundException for unrelated paths', function () { + Route::get('/_test/unrelated-missing-file', function () { + throw new FileNotFoundException( + 'File does not exist at path /var/www/html/storage/app/some-upload.pdf.' + ); + }); + + $response = $this->get('/_test/unrelated-missing-file'); + + expect($response->status())->not->toBe(503); +});