inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.03k stars 227 forks source link

Conflict with Laravel Nova #505

Closed CosmicodeStudio closed 10 months ago

CosmicodeStudio commented 1 year ago

I'm using SSR for a SPA with React and Laravel for backend. I'm using a default layout for all pages that I added in createInertiaApp in app.jsx and ssr.jsx. I'm using Laravel Nova for content management, the problem is SSR is giving me an error everytime I enter in Laravel Nova. I know Laravel Nova uses Inertia as well, but it shouldn't conflict with my project. The error that is giving me:

TypeError: Cannot read properties of undefined (reading 'default')
    at resolve (file:///var/www/html/bootstrap/ssr/ssr.mjs:3077:13)
    at f (file:///var/www/html/node_modules/@inertiajs/react/dist/index.esm.js:1:1512)
    at U (file:///var/www/html/node_modules/@inertiajs/react/dist/index.esm.js:1:1553)
    at file:///var/www/html/bootstrap/ssr/ssr.mjs:3047:13
    at /render (file:///var/www/html/node_modules/@inertiajs/core/dist/server.esm.js:1:295)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Server.<anonymous> (file:///var/www/html/node_modules/@inertiajs/core/dist/server.esm.js:1:527)

This line is where I have

page.default.layout =
                    page.default.layout ||
                    ((page) => <Layout children={page} />);

Have this happened to someone? I don't know why Laravel Nova is using my createInertiaApp.

RobertBoes commented 1 year ago

This is because Nova is essentially a different app, they have different build files etc. So instead of using Inertia to navigate to it you'd just use a normal anchor tag

CosmicodeStudio commented 1 year ago

The error is in the SSR daemon, running with php artisan inertia:start-ssr. It occurs every time someone goes to the backoffice, I don't have any link from the website to Laravel Nova.

moh-slimani commented 1 year ago

i'm having the same problem

The error is in the SSR daemon, running with php artisan inertia:start-ssr. It occurs every time someone goes to the backoffice, I don't have any link from the website to Laravel Nova.

CosmicodeStudio commented 1 year ago

Temporarily I'm checking if the page exists, and if not, I added an empty page.

let page = pages[`./Pages/${name}.jsx`];
if (!page) {
   page = pages[`./Pages/Empty.jsx`];
}
hjanos1 commented 1 year ago

We have solved it like this, just add a reference to this middleware right before HandleInertiaRequests::class in nova.php's middleware.web section

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

/**
 * The purpose of this middleware is to disable Inertia SSR for Nova, while keeping it enabled for the rest of the app
 * The middleware needs to be registered in config/nova.php for Nova web routes
 */
class DisableInertiaSSRForNovaMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        config()->set('inertia.ssr.enabled', false);

        return $next($request);
    }
}
reinink commented 10 months ago

Hey folks! Yup this is a known issue with Nova. Since Nova is built with Inertia.js, when you're running the Inertia SSR server and hit a Nova endpoint, the SSR server tries to render that page but fails, since Nova isn't setup for SSR (and it doesn't make any sense for it to be).

The way I have handled this in a project of mine that uses Inertia.js and Nova is by adding the following snippet to my AppServerProvider:

if (Request::is('nova/*')) {
    Config::set('inertia.ssr.enabled', false);
}

This will disable SSR just for the Nova endpoints and prevent this issue.

I've been meaning to ask the fine Nova folks to add this to Nova directly, and this is a good reminder to do that. (Pinging @jessarcher for help on this 😄 )

Going to close this issue in the meantime since it's not something that we're even able to fix in this library.

Hope that helps! 🤙