inertiajs / inertia-laravel

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

Add "always" props using new `Inertia::always()` wrapper #627

Closed lepikhinb closed 1 month ago

lepikhinb commented 1 month ago

Introduce Inertia::always() wrapper to replace the current persistent props implementation.

return [
    'flash' => Inertia::always(session('flash')),
];

Lazy data evaluation behavior:

return Inertia::render('Users/Index', [
    // ALWAYS included on first visit...
    // OPTIONALLY included on partial reloads...
    // ALWAYS evaluated...
    'users' => User::get(),

    // ALWAYS included on first visit...
    // OPTIONALLY included on partial reloads...
    // ONLY evaluated when needed...
    'users' => fn () => User::get(),

    // NEVER included on first visit...
    // OPTIONALLY included on partial reloads...
    // ONLY evaluated when needed...
    'users' => Inertia::lazy(fn () => User::get()),

    // ALWAYS included on first visit...
    // ALWAYS included on partial reloads...
    // ALWAYS evaluated...
    'users' => Inertia::always(User::get()),
]);
lepikhinb commented 1 month ago

@reinink made AlwaysProp accept any values including callable 👌🏻

reinink commented 1 month ago

@lepikhinb Hey thanks for putting this together! 💪

gabrielrbarbosa commented 3 weeks ago

@lepikhinb "errors" prop is empty with laravel validation after this update

datlechin commented 3 weeks ago

After updating to v1.3.0, no errors are included in the response; it is always empty.

This issue did not occur in v1.2.0.

@reinink, could you please look into this?

lepikhinb commented 3 weeks ago

@datlechin @gabrielrbarbosa I couldn't reproduce the issue. Is there a chance you can make a quick reproduction?

datlechin commented 3 weeks ago

@lepikhinb Create a route that performs data validation. Send a request to this route with invalid data and verify that the response does not contain any error messages.

lepikhinb commented 3 weeks ago

@datlechin Please make a repo with a complete reproduction.

Here's PingCRM with Inertia 1.3.

CleanShot 2024-06-22 at 18 36 43@2x

datlechin commented 3 weeks ago

Ah i see that what happened

Previously, I used the appendTo method to add Inertia’s middleware, which was works with versions prior to v1.3.0:

->withMiddleware(function (Middleware $middleware) {
    $middleware->appendTo(HandleInertiaRequests::class);
})

After updating to v1.3.0, the approach needs to be changed. Now, you must use the web method to add Inertia’s middleware.

gabrielrbarbosa commented 3 weeks ago

@lepikhinb I found out what happened here. We had previously overridden the Response class which made this new update incompatible, I will handle this here or just fix at 1.2.0 for now, thanks anyway!