Skip to content

Commit 948e4eb

Browse files
committed
Docs for Inertia::once()
1 parent a833796 commit 948e4eb

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
"v2/data-props/partial-reloads",
122122
"v2/data-props/deferred-props",
123123
"v2/data-props/merging-props",
124+
"v2/data-props/once-props",
124125
"v2/data-props/polling",
125126
"v2/data-props/prefetching",
126127
"v2/data-props/load-when-visible",

v2/data-props/deferred-props.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export default () => (
109109

110110
</CodeGroup>
111111

112+
## Multiple Deferred Props
113+
112114
If you need to wait for multiple deferred props to become available, you can specify an array to the `data` prop.
113115

114116
<CodeGroup>
@@ -173,3 +175,15 @@ export default () => (
173175
```
174176

175177
</CodeGroup>
178+
179+
## Combining with Once Props
180+
181+
You may chain the `once()` modifier onto a deferred prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.
182+
183+
```php
184+
return Inertia::render('Dashboard', [
185+
'stats' => Inertia::defer(fn () => Stats::generate())->once(),
186+
]);
187+
```
188+
189+
For more information on once props, see the [once props](/v2/data-props/once-props) documentation.

v2/data-props/merging-props.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ You can also merge props directly on the client side without making a server req
109109

110110
## Combining with Deferred Props
111111

112-
You can also combine [deferred props](/v2/data-props/deferred-props) with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.
112+
You may combine [deferred props](/v2/data-props/deferred-props) with mergeable props to defer the loading of the prop and ultimately mark it as mergeable once it's loaded.
113113

114114
```php
115115
Route::get('/users', function () {
@@ -122,6 +122,18 @@ Route::get('/users', function () {
122122
});
123123
```
124124

125+
## Combining with Once Props
126+
127+
You may chain the `once()` modifier onto a merge prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.
128+
129+
```php
130+
return Inertia::render('Users/Index', [
131+
'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
132+
]);
133+
```
134+
135+
For more information on once props, see the [once props](/v2/data-props/once-props) documentation.
136+
125137
## Resetting Props
126138

127139
On the client side, you can indicate to the server that you would like to reset the prop. This is useful when you want to clear the prop value before merging new data, such as when the user enters a new search query on a paginated list.

v2/data-props/once-props.mdx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
title: Once Props
3+
---
4+
5+
Some data rarely changes or is expensive to compute. Rather than resolving this data on every request, you may use _once props_. These props are remembered by the client and reused on subsequent pages that include the same prop. This makes them ideal for [shared data](/v2/data-props/shared-data).
6+
7+
## Creating Once Props
8+
9+
To create a once prop, use the `Inertia::once()` method when returning your response. This method receives a callback that returns the prop data.
10+
11+
```php
12+
return Inertia::render('Settings', [
13+
'countries' => Inertia::once(fn () => Country::all()),
14+
]);
15+
```
16+
17+
Once 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.
18+
19+
Keep in mind that navigating to a page that doesn't include the once prop will break the chain. The backend will resolve the prop again when you navigate to a page that includes it.
20+
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+
49+
## Forcing a Refresh
50+
51+
You may force a once prop to be refreshed using the `fresh()` method.
52+
53+
```php
54+
return Inertia::render('Settings', [
55+
'countries' => Inertia::once(fn () => Country::all())->fresh(),
56+
]);
57+
```
58+
59+
This method accepts a boolean, allowing you to conditionally refresh the prop.
60+
61+
```php
62+
return Inertia::render('Settings', [
63+
'preferences' => Inertia::once(fn () => $user->preferences)->fresh($user->wasChanged()),
64+
]);
65+
```
66+
67+
## Setting an Expiration
68+
69+
You may set an expiration time using the `until()` method. This method accepts a `DateTimeInterface`, `DateInterval` instance, or an integer number of seconds. The prop will be refreshed on a subsequent visit after the expiration time has passed.
70+
71+
```php
72+
return Inertia::render('Dashboard', [
73+
'rates' => Inertia::once(fn () => ExchangeRate::all())->until(now()->addHour()),
74+
]);
75+
```
76+
77+
## Custom Keys
78+
79+
You may assign a custom key using the `as()` method. This is useful when you want to share data across multiple pages while using different prop names.
80+
81+
```php
82+
// Checkout page
83+
return Inertia::render('Checkout', [
84+
'shippingCountries' => Inertia::once(fn () => Country::all())->as('countries'),
85+
]);
86+
87+
// Settings page
88+
return Inertia::render('Settings', [
89+
'availableCountries' => Inertia::once(fn () => Country::all())->as('countries'),
90+
]);
91+
```
92+
93+
Both pages share the same underlying data because they use the same custom key. The prop will only be resolved once, even though the prop names differ.
94+
95+
## Prefetching
96+
97+
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.
98+
99+
The prefetch cache also respects once prop expiration. Prefetched pages containing an expired once prop will be invalidated.
100+
101+
## Combining with Other Prop Types
102+
103+
The `once()` modifier may be chained onto [deferred](/v2/data-props/deferred-props), [merge](/v2/data-props/merging-props), and [optional](/v2/data-props/partial-reloads#lazy-data-evaluation) props.
104+
105+
```php
106+
return Inertia::render('Dashboard', [
107+
'countries' => Inertia::defer(fn () => Country::all())->once(),
108+
'activity' => Inertia::merge(fn () => $user->recentActivity())->once(),
109+
'categories' => Inertia::optional(fn () => Category::all())->once(),
110+
]);
111+
```

v2/data-props/partial-reloads.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ Here's a summary of each approach:
163163
| <span style={{whiteSpace: 'nowrap'}}>`fn () => User::all()`</span> | Always | Optionally | Only when needed | |
164164
| <span style={{whiteSpace: 'nowrap'}}>`Inertia::optional(fn () => User::all())`</span> | Never | Optionally | Only when needed | |
165165
| <span style={{whiteSpace: 'nowrap'}}>`Inertia::always(fn () => User::all())`</span> | Always | Always | Always | |
166+
167+
## Combining with Once Props
168+
169+
You may chain the `once()` modifier onto an optional prop to ensure the data is resolved only once and remembered by the client across subsequent navigations.
170+
171+
```php
172+
return Inertia::render('Users/Index', [
173+
'users' => Inertia::optional(fn () => User::all())->once(),
174+
]);
175+
```
176+
177+
For more information on once props, see the [once props](/v2/data-props/once-props) documentation.

v2/data-props/shared-data.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,42 @@ Shared data should be used sparingly as all shared data is included with every r
4747

4848
Page props and shared data are merged together, so be sure to namespace your shared data appropriately to avoid collisions.
4949

50+
## Sharing Once Props
51+
52+
You may share data that is resolved only once and remembered by the client across subsequent navigations using [once props](/v2/data-props/once-props).
53+
54+
```php
55+
class HandleInertiaRequests extends Middleware
56+
{
57+
public function share(Request $request)
58+
{
59+
return array_merge(parent::share($request), [
60+
'countries' => Inertia::once(fn () => Country::all()),
61+
]);
62+
}
63+
}
64+
```
65+
66+
Alternatively, you may define a dedicated `shareOnce()` method in the middleware.
67+
68+
```php
69+
class HandleInertiaRequests extends Middleware
70+
{
71+
public function shareOnce(Request $request): array
72+
{
73+
return [
74+
'countries' => fn () => Country::all(),
75+
];
76+
}
77+
}
78+
```
79+
80+
You may also share once props manually using the `Inertia::shareOnce()` helper.
81+
82+
```php
83+
Inertia::shareOnce('countries', fn () => Country::all());
84+
```
85+
5086
## Accessing Shared Data
5187

5288
Once you have shared the data server-side, you will be able to access it within any of your pages or components. Here's an example of how to access shared data in a layout component.

0 commit comments

Comments
 (0)