Component JS/CSS asset routes return 500 instead of 404 when the asset doesn't exist
Livewire version
v4.3.3
Laravel version
v13.18.1
PHP version
8.4.23
Browser and operating system
curl/8.7.1 on macOS (no browser involved — these are plain GET requests)
Describe the issue you're experiencing
The three per-component asset routes resolve the component straight out of the URL and throw on every miss, so a request for an asset that isn't there comes back as a 500 instead of a 404:
| Request | Thrown in | Status |
|---|---|---|
/livewire-{hash}/js/{unknown}.js |
LivewireManager::new() → ComponentNotFoundException |
500 |
/livewire-{hash}/js/{real-component}.js (component has no script module) |
Exception('Component ... does not have a script source.') |
500 |
/livewire-{hash}/css/{real-component}.css |
Exception('... does not have a style source.') |
500 |
/livewire-{hash}/css/{real-component}.global.css |
Exception('... does not have a global style source.') |
500 |
The endpoints are SupportJsModules::provide() and SupportCssModules::provide():
Two consequences in production:
- It pages you. A single
curl https://example.com/livewire-{hash}/js/x.jsraised aComponentNotFoundExceptionalert in Laravel Nightwatch, reported as a 500 HTTP request. The{hash}prefix is derived fromAPP_KEY, but it ships in the<script src>of every page that renders Livewire, so anything that has loaded one public page can hit these routes. - Every real component 500s too, not just invented names. The overwhelmingly common case is
a component with no
scriptModuleSrc()/styleModuleSrc()at all, and that is the branch that throws a bare\Exception— so an app cannot even filter it by exception class.
Both are "the asset isn't there", which is what 404 means. Nothing in these two closures does anything but read a file off disk, so there is no failure mode here that a 500 describes correctly.
Code snippets to reproduce the issue
Fresh Laravel 13 app with livewire/livewire:^4.3, one ordinary component:
// app/Livewire/Counter.php
<?php
namespace App\Livewire;
use Livewire\Component;
class Counter extends Component
{
public function render()
{
return <<<'HTML'
<div>Hello</div>
HTML;
}
}Grab the endpoint prefix (it is in the <script src> of any page rendering Livewire, or:
php artisan tinker --execute 'echo \Livewire\Mechanisms\HandleRequests\EndpointResolver::prefix();'
# /livewire-a1b2c3d4Then:
# 1. a component that does not exist — ComponentNotFoundException
curl -i https://example.test/livewire-a1b2c3d4/js/nope.js
# HTTP/1.1 500 Internal Server Error ("Unable to find component: [nope]")
# 2. the real component above, which simply has no script module
curl -i https://example.test/livewire-a1b2c3d4/js/counter.js
# HTTP/1.1 500 Internal Server Error ("Component counter does not have a script source.")
# 3. same for both CSS endpoints
curl -i https://example.test/livewire-a1b2c3d4/css/counter.css
curl -i https://example.test/livewire-a1b2c3d4/css/counter.global.css
# HTTP/1.1 500 Internal Server ErrorAll four are 500 with APP_DEBUG either on or off.
How do you expect it to work?
All four should be 404. The requested asset does not exist; that is a client error, not a server fault, and it should not reach the app's exception reporting.
A minimal shape, in SupportJsModules::provide() (and the same in SupportCssModules::provide()
for both of its routes):
try {
$instance = app('livewire')->new($component);
} catch (ComponentNotFoundException) {
abort(404);
}
if (! method_exists($instance, 'scriptModuleSrc')) {
abort(404);
}
$path = $instance->scriptModuleSrc();
if (! file_exists($path)) {
abort(404);
}Happy to open a PR along those lines if the approach looks right.
For anyone who lands here first, the app-side workaround is to render these paths as 404 in
bootstrap/app.php — the route closures live in the vendor package and are registered before the
application's own routes, so they cannot be overridden by re-declaring the URI:
->withExceptions(function (Exceptions $exceptions): void {
$isLivewireComponentAsset = function (Request $request): bool {
$prefix = trim(EndpointResolver::prefix(), '/');
return $request->is("{$prefix}/js/*", "{$prefix}/css/*");
};
$exceptions->render(fn (Throwable $e, Request $request) => $isLivewireComponentAsset($request)
? response('', 404)
: null);
$exceptions->dontReportWhen(fn (Throwable $e) => $isLivewireComponentAsset(request()));
})Please confirm
- I have provided easy and step-by-step instructions to reproduce the bug.
- I have provided code samples as text and NOT images.
- I understand my bug report will be removed if I haven't met the criteria above.
Source: livewire/livewire