inertiajs / inertia-laravel

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

Add Defer Prop #531

Closed adrum closed 5 months ago

adrum commented 1 year ago

I felt inspired by Livewire 3 Lazy feature which automatically fetched the data on page load. I think there's a ton of value in Inertia's current Lazy prop feature, but I think there are also scenarios where adding a Defer prop feature which automatically loads Lazy data on page load.

This works in tandem with https://github.com/inertiajs/inertia/pull/1617. It works by extending the Lazy feature but also informs the front-end on page load which deferred properties should be loaded automatically. These properties will then automatically trigger a reload with those properties automatically added in the only attribute.

This reduces the amount of front-end code required to pull this off. To add a long-running API query to be deferred, it would look like this:

return Inertia::render('Organizations/Index', [
            'filters' => Request::all('search', 'trashed'),
            'organizations' => Inertia::defer(
                fn () =>
                Auth::user()->account->organizations()
                ->orderBy('name')
                ->filter(Request::only('search', 'trashed'))
                ->paginate(10)
                ->withQueryString()
                ->through(fn ($organization) => [
                    'id' => $organization->id,
                    'name' => $organization->name,
                    'phone' => $organization->phone,
                    'city' => $organization->city,
                    'deleted_at' => $organization->deleted_at,
                ])
            ),
        ]);

These deferred properties will only ever be loaded automatically after page loads/navigations. Otherwise, they behave just the same as Lazy props.

samehdoush commented 1 year ago

It's great, waiting for its acceptance, so we can start using it immediately 👍 💯

adrum commented 8 months ago

I just resolved the merge conflicts associated with this PR. Let me know if we want/need anything else.

adrum commented 8 months ago

I'm having difficulty reproducing the failed test locally. 🤔 Anything I should try?

rojtjo commented 8 months ago

It looks like the assertObjectNotHasAttribute was removed in v10 and assertObjectNotHasProperty was added as a replacement in v10.1 and v9.6.

Easiest solution is to just use assertFalse in combination with a property_exists check.

See: https://github.com/sebastianbergmann/phpunit/issues/5478

adrum commented 8 months ago

@rojtjo Thank you for getting to the bottom of that! I did not dig far enough into the error message to recognize the removal of those methods from PHPUnit.

chuck-waters commented 8 months ago

This will solve so many things for us. Great addition!