laravel with inertiajs - practice i just used for inertia projects to prevent repetitively executing inertia share method

 green surface with holes

image :

 Hello guys , i just thought to share my little experience in inertia js.

inertia js( getting quite popular with laravel now.  I had develop web project with inertia js. its easy and handle lot of Ajax tasks through the package. this helped lot  to create single page like application. but i faced issue is , there is a middlware calllded 'HandleInertiaRequests' and it have method callded 'share' . this share method calling every time we running a request through inertia service . I had created some sql query running tasks in this 'share' method, for update UI according to the updated data. things worked normally. Later I had created some ajax calling with separate routes which have their own tasks to update ui. but that not connected with inertiajs. but when I checking that ajax requests with "laravel debugbar" I saw there are lot of query executing for a little ajax call. how posbile that? . then I found the inertiajs 'share' method also executing with the every ajax call in route( "@inertiajs/inertia": "^0.8.7",  "inertiajs/inertia-laravel": "^0.3.2").  the inertia js covered with every request in 'web.php' file in route folder. so I had to find a way to skip without affecting current working progress. What I just did thing is, remove it from 'web' list and, add separately as a named middlware in 'App\Http\Kernel' file. then inject it to web routes as separate middlware. then remove 'inertia' middlware for special routes using 'withoutMiddleware'. So I able to create ajax routes that  doesn't   go though ineritajs middlware. here the steps I have done.

in 'App\Http\Kernel.php'  file

first remove it from The application's route middleware groups.

* The application's route middleware groups.
* @var array
protected $middlewareGroups = [
'web' => [
// \Illuminate\Session\Middleware\AuthenticateSession::class,

then add it as a separate middleware. 

* The application's route middleware.
* These middleware may be assigned to groups or used individually.
* @var array
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'customer.auth' => \App\Http\Middleware\CustomerAuthentication::class,
'inertia' => \App\Http\Middleware\HandleInertiaRequests::class,
//added as a separate Middleware to routes service provider

in 'App\Providers\RouteServiceProvider.php' file

in boot method:

* Define your route model bindings, pattern filters, etc.
* @return void
public function boot()

$this->routes(function () {

//seprately included inertia midleware

now almost done. but still every web route requests still calling  inertia middlware.

now just need to remove inertia middlware from special "POST' routes and ajax routes.

in 'Routes\web.php' file you can group special routes and remove inertia from it.

//ajax routes :: without intertia midleware
Route::prefix('ajax')->name('ajax.')->group(function () {
'excluded_middleware' => ['inertia'],
], function () {
Route::get('ajax-route-1', [AjaxController::class, 'method1']);
Route::get('ajax-route-2', [AjaxController::class, 'method2']);

for a single route you can remove like this:

Route::post('post-request', [CritcalController::class, 'method'])

that's it 🎉

.. thanks for searching for this though :)


Popular posts from this blog

Handling undefined variables in laravel

How to use Spatie packages to enhance your Laravel development experience

Some Briefs of Data Mining