Skip to content

Commit 1cbabc1

Browse files
committed
Add support for Inertia deferred props.
1 parent 83d1d42 commit 1cbabc1

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

README.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ We assume you've already got the Inertia adapter for Laravel installed.
2525
Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and
2626
our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views?
2727
28-
This is especially evident in Inertia applications due to the introduction of concepts like lazy props.
28+
This is especially evident in Inertia applications due to the introduction of concepts like lazy and deferred props.
2929
3030
Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly
3131
useful if multiple routes or controllers within your application always needs a particular piece of data.
@@ -65,15 +65,36 @@ class DemoController extends Controller
6565
Data providers can live anywhere, but we'll use `App/Http/DataProviders` for this example.
6666

6767
The simplest data provider is just a class that extends `DataProvider`, any public methods or properties will be available to the page as data.
68+
```php
69+
<?php
70+
declare(strict_types=1);
71+
72+
namespace App\Http\DataProviders;
73+
74+
use Inertia\LazyProp;
75+
use App\Services\InjectedDependency;
76+
use Webfox\InertiaDataProviders\DataProvider;
77+
78+
class DemoDataProvider extends DataProvider
79+
{
80+
public string $someData = 'data';
81+
82+
public function moreData(): int
83+
{
84+
return time();
85+
}
86+
}
87+
```
6888

69-
A **kitchen sink** data provider might look like this:
89+
A full **kitchen sink** data provider might look like this:
7090

7191
```php
7292
<?php
7393
declare(strict_types=1);
7494
7595
namespace App\Http\DataProviders;
7696
97+
use Inertia\DeferProp;
7798
use Inertia\LazyProp;
7899
use App\Services\InjectedDependency;
79100
use Webfox\InertiaDataProviders\DataProvider;
@@ -116,16 +137,16 @@ class DemoDataProvider extends DataProvider
116137
}
117138
118139
/*
119-
* If a method returns a `Closure` it will be evaluated as a lazy property.
120-
* ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
121-
* Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
122-
* @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
140+
* If a method is typed to return a DeferProp, it will only be evaluated in a deferred request after the page has loaded
141+
* NEVER included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated after the page has loaded.
142+
* Additionally the deferred callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
143+
* @see https://inertiajs.com/deferred-props
123144
*/
124-
public function quickLazyExample(): Closure
145+
public function deferredExample(): DeferProp
125146
{
126-
return function(InjectedDependency $example): string {
127-
return $example->formatName($this->demo->user->name);
128-
};
147+
return Inertia::defer(
148+
fn () => $this->demo->aHeavyCalculation()
149+
);
129150
}
130151
131152
/*
@@ -141,6 +162,19 @@ class DemoDataProvider extends DataProvider
141162
);
142163
}
143164
165+
/*
166+
* If a method returns a `Closure` it will be evaluated as a lazy property.
167+
* ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed
168+
* Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved.
169+
* @see https://inertiajs.com/partial-reloads#lazy-data-evaluation
170+
*/
171+
public function quickLazyExample(): Closure
172+
{
173+
return function(InjectedDependency $example): string {
174+
return $example->formatName($this->demo->user->name);
175+
};
176+
}
177+
144178
/*
145179
* `protected` and `private` methods are not available to the page
146180
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"illuminate/contracts": "^11.0"
2525
},
2626
"require-dev": {
27-
"inertiajs/inertia-laravel": "<2",
27+
"inertiajs/inertia-laravel": "^1.0|^2.0",
2828
"jetbrains/phpstorm-attributes": "^1.0",
2929
"nunomaduro/collision": "^v8.1",
3030
"larastan/larastan": "^v2.9",

src/DataProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use Illuminate\Contracts\Support\Arrayable;
99
use Illuminate\Contracts\Support\Jsonable;
10+
use Inertia\DeferProp;
1011
use Inertia\LazyProp;
1112
use Inertia\Response;
1213
use ReflectionClass;
@@ -41,7 +42,7 @@ public function toArray(): array
4142
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods))
4243
->mapWithKeys(function (ReflectionMethod $method) {
4344
$returnType = $method->getReturnType();
44-
if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [LazyProp::class, Closure::class])) {
45+
if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) {
4546
return [$method->name => $method->invoke($this)];
4647
}
4748

0 commit comments

Comments
 (0)