Skip to content

Commit 31d0051

Browse files
committed
Update once-props.mdx
1 parent cd86baa commit 31d0051

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

v2/data-props/once-props.mdx

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,30 @@ Some data rarely changes or is expensive to compute. Rather than resolving this
99
To create a once prop, use the `Inertia::once()` method when returning your response. This method receives a callback that returns the prop data.
1010

1111
```php
12-
return Inertia::render('Settings', [
13-
'countries' => Inertia::once(fn () => Country::all()),
12+
return Inertia::render('Billing', [
13+
'plans' => Inertia::once(fn () => Plan::all()),
1414
]);
1515
```
1616

1717
After the client has received this prop, subsequent requests will skip resolving the callback entirely and the prop will be excluded from the response. The client will automatically reuse the remembered value.
1818

1919
Navigating to a page without the once prop will break the chain, and the prop will be resolved again on the next page that has it.
2020

21-
## Sharing Once Props
22-
23-
You may share once props globally using the `Inertia::share()` method.
24-
25-
```php
26-
Inertia::share('countries', Inertia::once(fn () => Country::all()));
27-
```
28-
29-
Or use the `shareOnce()` helper method.
30-
31-
```php
32-
Inertia::shareOnce('countries', fn () => Country::all());
33-
```
34-
35-
You may also define a dedicated `shareOnce()` method in your middleware.
36-
37-
```php
38-
class HandleInertiaRequests extends Middleware
39-
{
40-
public function shareOnce(Request $request): array
41-
{
42-
return [
43-
'countries' => fn () => Country::all(),
44-
];
45-
}
46-
}
47-
```
48-
4921
## Forcing a Refresh
5022

5123
You may force a once prop to be refreshed using the `fresh()` method.
5224

5325
```php
54-
return Inertia::render('Settings', [
55-
'countries' => Inertia::once(fn () => Country::all())->fresh(),
26+
return Inertia::render('Billing', [
27+
'plans' => Inertia::once(fn () => Plan::all())->fresh(),
5628
]);
5729
```
5830

59-
This method accepts a boolean, allowing you to conditionally refresh the prop.
31+
This method also accepts a boolean, allowing you to conditionally refresh the prop.
6032

6133
```php
62-
return Inertia::render('Settings', [
63-
'preferences' => Inertia::once(fn () => $user->preferences)->fresh($user->wasChanged()),
34+
return Inertia::render('Billing', [
35+
'plans' => Inertia::once(fn () => Plan::all())->fresh($condition),
6436
]);
6537
```
6638

@@ -92,6 +64,40 @@ return Inertia::render('Team/Invite', [
9264

9365
Both pages share the same underlying data because they use the same custom key, so the prop is only resolved for whichever page you visit first.
9466

67+
## Sharing Once Props
68+
69+
You may share once props globally using the `Inertia::share()` method.
70+
71+
```php
72+
Inertia::share('countries', Inertia::once(fn () => Country::all()));
73+
```
74+
75+
Or use the `shareOnce()` helper method.
76+
77+
```php
78+
Inertia::shareOnce('countries', fn () => Country::all());
79+
```
80+
81+
You may also chain `as()`, `fresh()`, and `until()` onto this method.
82+
83+
```php
84+
Inertia::shareOnce('countries', fn () => Country::all())->until(3600);
85+
```
86+
87+
You may also define a dedicated `shareOnce()` method in your middleware.
88+
89+
```php
90+
class HandleInertiaRequests extends Middleware
91+
{
92+
public function shareOnce(Request $request): array
93+
{
94+
return [
95+
'countries' => fn () => Country::all(),
96+
];
97+
}
98+
}
99+
```
100+
95101
## Prefetching
96102

97103
Once props work with [prefetching](/v2/data-props/prefetching). The client automatically includes any remembered once props in prefetched responses, so navigating to a prefetched page will already have the once props available.
@@ -104,7 +110,7 @@ The `once()` modifier may be chained onto [deferred](/v2/data-props/deferred-pro
104110

105111
```php
106112
return Inertia::render('Dashboard', [
107-
'countries' => Inertia::defer(fn () => Country::all())->once(),
113+
'permissions' => Inertia::defer(fn () => Permission::all())->once(),
108114
'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
109115
'categories' => Inertia::optional(fn () => Category::all())->once(),
110116
]);

0 commit comments

Comments
 (0)