Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Defer Prop #531

Closed
wants to merge 5 commits into from
Closed

Add Defer Prop #531

wants to merge 5 commits into from

Conversation

adrum
Copy link

@adrum adrum commented Jul 23, 2023

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 inertiajs/inertia#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
Copy link

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

# Conflicts:
#	src/Inertia.php
# Conflicts:
#	tests/ResponseFactoryTest.php
@adrum
Copy link
Author

adrum commented Feb 2, 2024

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

@adrum
Copy link
Author

adrum commented Feb 2, 2024

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

tests/ResponseTest.php Outdated Show resolved Hide resolved
tests/ResponseTest.php Outdated Show resolved Hide resolved
@rojtjo
Copy link
Contributor

rojtjo commented Feb 2, 2024

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: sebastianbergmann/phpunit#5478

Co-authored-by: Roj Vroemen <me@rojvroemen.com>
@adrum
Copy link
Author

adrum commented Feb 2, 2024

@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
Copy link

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

@driesvints driesvints deleted the branch inertiajs:master May 15, 2024 15:01
@driesvints driesvints closed this May 15, 2024
@adrum adrum mentioned this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants