Skip to content

Commit

Permalink
When a 404 exception is thrown, don't rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 committed Jan 13, 2025
1 parent ba347af commit e475a2e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Http/Controllers/FallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Exceptions;
use Rapidez\Core\Facades\Rapidez;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Throwable;

class FallbackController extends Controller
{
Expand All @@ -33,6 +35,17 @@ public function __invoke(Request $request)
{
$cacheKey = 'fallbackroute-' . md5($request->url());
$route = Cache::get($cacheKey);

Exceptions::shouldRenderJsonWhen(function ($request, Throwable $exception) {
return collect([
RouteNotFoundException::class,
NotFoundHttpException::class,
BackedEnumCaseNotFoundException::class,
ModelNotFoundException::class,
RecordsNotFoundException::class,
])->contains(fn($class) => $exception instanceof $class) || $request->expectsJson();
});

if ($route && $response = $this->tryRoute($route['route'], $request)) {
return $response;
}
Expand All @@ -51,7 +64,8 @@ public function __invoke(Request $request)

return $response;
}


Exceptions::shouldRenderJsonWhen(null);
abort(404);
}

Expand Down

0 comments on commit e475a2e

Please sign in to comment.