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' => [

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!

Author: Daron

Professional software developer with extensive experience in Laravel and WordPress. When not writing code, he enjoys Netflix and playing Nintendo. He is also available for freelance opportunities. Get in touch to hash out the details.

Leave a Reply

Your email address will not be published.