corsinertiajslaravel-10laravel-nova

Laravel 10 / Nova 4 - Inertia requests must receive a valid Inertia response


I have been racking my brains for days over this problem and could not fix it yet. I would be so appreciate if someone could help me!

# Current Environment (Local Docker)
"php": "8.1"
"laravel/nova": "4.35.6"
"laravel/framework": "10.45.28"

After upgrading my application from Laravel 9 to Laravel 10 by following Upgrade Guide, I’ve encountered an issue with Laravel Nova’s UI. When I click on a sub-navigation item, I receive the following error:

"All Inertia requests must receive a valid Inertia response, however a plain JSON response was received."

Laravel Nova opening sub-navigation items

It appears that Nova is returning its usual JSON payload (containing configuration and menu data) while Inertia expects a proper Inertia response. I suspect this might be due to middleware interference or a misconfiguration with CORS. The subpages (like /resources/products ) can be still accessed by entering the URL directly.

STEPS DONE

CORS CONFIGURATION

Following a recommendation in StackOverflow Post1 and Post2, I added 'exposed_headers' => ['x-inertia'] to my config/cors.php. However, the Access-Control-Expose-Headers: x-inertia header is not showing up in my network responses. Maybe Nova is overriding it?

CORS MIDDLEWARE

Replaced Laravel 10, built-in CORS with the Fruitcake package fruitcake/laravel-cors

DEPENDENCY UPDATES

php: "8.*" → "^8.1"
doctrine/dbal: "^2.8" → "^3.0"
laravel/framework: "9.*" → "10.*"
laravel/nova: "4.25.1" → "^4.35"
fruitcake/laravel-cors: "^2.0" → (removed)
spatie/laravel-permission: "5.*" → "^6.0"
auth0/auth0-php: "8.3.1" → "^8.10"
auth0/login: "7.1" → "^7.8"
itsmejoshua/novaspatiepermissions: "^1.0" → (removed)
kiritokatklian/nova-permission: (not present) → "*"
# ...

CMDs

Deleted /vendor and node_modules and executed:

composer update
npm i && npm run dev
php artisan route:clear && php artisan config:clear && php artisan cache:clear
php artisan optimize:clear
php artisan nova:publish
    

CLASSES

/config/auth.php

// ...
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
],
 'providers' => [
        'users' => [
            'driver' =>  'eloquent',
            'model' => App\Models\User::class,
        ],
 ]

/config/cors.php

return [
    'paths' => ['*'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*', 'http://localhost:3000', env('APP_URL')],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*','content-type,x-inertia,x-inertia-version,x-requested-with'],
    'exposed_headers' => ['x-inertia'],
    'max_age' => 0,
    'supports_credentials' => true,
];

/config/nova.php

// ...
 'middleware' => [
        'web',
        HandleInertiaRequests::class,
        DispatchServingNovaEvent::class,
        BootTools::class,
        \Vyuldashev\NovaPermission\ForgetCachedPermissions::class,
    ],

app/Http/Kernal.php

// ...
 protected $middleware = [
        \Laravel\Nova\Http\Middleware\HandleInertiaRequests::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

/package.json

// ...
"devDependencies": {
        "axios": "^0.27.2",
        "cross-env": "^7.0.3",
        "laravel-mix": "^6.0.49",
        "lodash": "^4.17.13",
        "postcss": "^8.3.11",
        "resolve-url-loader": "^5.0.0",
        "sass": "^1.54.9",
        "sass-loader": "^12.6.0",
        "vue": "^3.2.39",
        "vue-loader": "^16.8.3",
        "vue-template-compiler": "^2.6.11",
        "vuex": "^4.0.2",                                                                                                                                                                               
        ....
    },

I’m at a loss. Did I miss any additional configuration steps or middleware adjustments required to ensure Nova’s JSON responses aren’t being misinterpreted? Any other troubleshooting steps that might resolve this?

Thank you for your assistance.

See a more detailed problem description in this post in the laravel forum.

Best regards!


Solution

  • As suggested in the GitHub Discussion, it was a problem with the webserver configuration. I'm using a custom docker image and nginx proxy on local. I was able to fix it by adding the header in nginx.conf:

    add_header X-Inertia "true";