mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-app.git
synced 2026-05-04 04:34:54 +00:00
✨ **Middleware & Tests:** Improved exception handling for stale Livewire asset requests to return 404 instead of 500. Added feature tests to validate these scenarios. 🚀
This commit is contained in:
+31
-4
@@ -1,8 +1,12 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Middleware\DomainMiddleware;
|
||||
use App\Http\Middleware\SetTimezone;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Configuration\Exceptions;
|
||||
use Illuminate\Foundation\Configuration\Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
use Stefro\LaravelLangCountry\Middleware\LangCountrySession;
|
||||
|
||||
return Application::configure(basePath: dirname(__DIR__))
|
||||
->withRouting(
|
||||
@@ -13,11 +17,34 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
)
|
||||
->withMiddleware(function (Middleware $middleware) {
|
||||
$middleware->web(append: [
|
||||
\App\Http\Middleware\DomainMiddleware::class,
|
||||
\Stefro\LaravelLangCountry\Middleware\LangCountrySession::class,
|
||||
\App\Http\Middleware\SetTimezone::class,
|
||||
DomainMiddleware::class,
|
||||
LangCountrySession::class,
|
||||
SetTimezone::class,
|
||||
]);
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions) {
|
||||
//
|
||||
$exceptions->render(function (Throwable $e, Request $request) {
|
||||
if (! preg_match('#^livewire-[a-f0-9]+/(?:css|js)/#', $request->path())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$message = $e->getMessage();
|
||||
|
||||
$stalePatterns = [
|
||||
'does not have a style source',
|
||||
'does not have a global style source',
|
||||
'does not have a script source',
|
||||
'Style file not found',
|
||||
'Global style file not found',
|
||||
'Script file not found',
|
||||
];
|
||||
|
||||
foreach ($stalePatterns as $pattern) {
|
||||
if (str_contains($message, $pattern)) {
|
||||
return response('', 404);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
})->create();
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Livewire\Mechanisms\HandleRequests\EndpointResolver;
|
||||
|
||||
it('returns 404 for stale livewire css module urls instead of 500', function () {
|
||||
$prefix = EndpointResolver::prefix();
|
||||
|
||||
$response = $this->get($prefix.'/css/meetups--landingpage.css?v=1502173559');
|
||||
|
||||
$response->assertNotFound();
|
||||
});
|
||||
|
||||
it('returns 404 for stale livewire global css module urls', function () {
|
||||
$prefix = EndpointResolver::prefix();
|
||||
|
||||
$response = $this->get($prefix.'/css/meetups--landingpage.global.css?v=1');
|
||||
|
||||
$response->assertNotFound();
|
||||
});
|
||||
|
||||
it('returns 404 for stale livewire js module urls', function () {
|
||||
$prefix = EndpointResolver::prefix();
|
||||
|
||||
$response = $this->get($prefix.'/js/meetups--landingpage.js?v=1');
|
||||
|
||||
$response->assertNotFound();
|
||||
});
|
||||
|
||||
it('still serves css for components that do have a style source', function () {
|
||||
$componentName = '__staletest_with_style_'.bin2hex(random_bytes(4));
|
||||
$bladePath = resource_path('views/livewire/'.$componentName.'.blade.php');
|
||||
|
||||
file_put_contents($bladePath, <<<'BLADE'
|
||||
<?php
|
||||
|
||||
use Livewire\Component;
|
||||
|
||||
new class extends Component {
|
||||
public function render(): string
|
||||
{
|
||||
return '<div>test</div>';
|
||||
}
|
||||
}; ?>
|
||||
|
||||
<div>test</div>
|
||||
<style>
|
||||
.foo { color: red; }
|
||||
</style>
|
||||
BLADE
|
||||
);
|
||||
|
||||
try {
|
||||
$prefix = EndpointResolver::prefix();
|
||||
|
||||
$response = $this->get($prefix.'/css/'.$componentName.'.css');
|
||||
|
||||
$response->assertSuccessful();
|
||||
$response->assertHeader('content-type', 'text/css; charset=utf-8');
|
||||
expect($response->getContent())->toContain('.foo')->toContain('color: red');
|
||||
} finally {
|
||||
@unlink($bladePath);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user