Serving Requests without Sessions in Laravel using Inertia.js

This article serves as an addendum to the excellent article by Chris Fidao over on Laravel News.

When using Inertia, you may still want to serve some content without generating a session for an anonymous user.

# app/Http/Kernel.php
'static' => [
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \App\Http\Middleware\HandleInertiaRequests::class,
],

However, if your Inertia middleware relies on any session data, stuff could blow up!

Session store not set on request error

A quick fix is to return early in the share method of your HandleInertiaRequests middleware.

public function share(Request $request)
{
    if (!$request->hasSession()) {
        return [];
    }
    // ... do the rest of your sharing stuff here.
}

That's it! Happy developing!

Posted in:

Laravel VueJS Web Development