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

 green surface with holes

image : https://unsplash.com/photos/TNO9gxHhhj0

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

inertia js(https://inertiajs.com/) 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' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\HandleInertiaRequests::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->configureRateLimiting();

$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));

//seprately included inertia midleware
Route::middleware(['web','inertia'])
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}


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 () {
Route::group([
'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:



//withoutMiddleware
Route::post('post-request', [CritcalController::class, 'method'])
->withoutMiddleware('inertia')->name('name_for_metod');




that's it 🎉

.. thanks for searching for this though :)










Comments

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