|
| 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 | +``` |
0 commit comments