laravel with inertiajs - practice i just used for inertia projects to prevent repetitively executing inertia share method
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.
then add it as a separate middleware.
in 'App\Providers\RouteServiceProvider.php' file
in boot method:
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.
for a single route you can remove like this:
that's it 🎉
.. thanks for searching for this though :)
Comments
Post a Comment