Skip to content

Commit

Permalink
Add cache check before registering routes
Browse files Browse the repository at this point in the history
If routes are already cached, it is not necessary to re-register Horizon routes on every request.

I've found that many Laravel packages don't check for already cached routes and will needlessly re-register routes that already exist in the cache anyway.  This reduces boot times in production by ~0.5 ms which adds up when applied to multiple other first-party Laravel packages.

I can open PRs in other projects if this is approved.
  • Loading branch information
serpentblade committed Jan 15, 2024
1 parent f6fefaf commit cfe9920
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Horizon;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -50,6 +51,10 @@ protected function registerEvents()
*/
protected function registerRoutes()
{
if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
return;
}

Route::group([
'domain' => config('horizon.domain', null),
'prefix' => config('horizon.path'),
Expand Down

0 comments on commit cfe9920

Please sign in to comment.