|
| 1 | +# Once Props |
| 2 | + |
| 3 | +@available_since rails=master core=2.2.20 |
| 4 | + |
| 5 | +Some data rarely changes, is expensive to compute, or is simply large. Rather than including this data in every response, 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](/guide/shared-data). |
| 6 | + |
| 7 | +## Creating Once Props |
| 8 | + |
| 9 | +To create a once prop, use the `InertiaRails.once` method when returning your response. This method receives a block that returns the prop data. |
| 10 | + |
| 11 | +```ruby |
| 12 | +class BillingController < ApplicationController |
| 13 | + def index |
| 14 | + render inertia: { |
| 15 | + plans: InertiaRails.once { Plan.all }, |
| 16 | + } |
| 17 | + end |
| 18 | +end |
| 19 | +``` |
| 20 | + |
| 21 | +After the client has received this prop, subsequent requests will skip resolving the block and exclude the prop from the response. The client only remembers once props while navigating between pages that include them. |
| 22 | + |
| 23 | +Navigating to a page without the once prop will forget the remembered value, and it will be resolved again on the next page that has it. In practice, this is rarely an issue since once props are typically used as shared data or within a specific section of your application. |
| 24 | + |
| 25 | +## Forcing a Refresh |
| 26 | + |
| 27 | +You may force a once prop to be refreshed using the `fresh` parameter. |
| 28 | + |
| 29 | +```ruby |
| 30 | +class BillingController < ApplicationController |
| 31 | + def index |
| 32 | + render inertia: { |
| 33 | + plans: InertiaRails.once(fresh: true) { Plan.all }, |
| 34 | + } |
| 35 | + end |
| 36 | +end |
| 37 | +``` |
| 38 | + |
| 39 | +## Refreshing from the Client |
| 40 | + |
| 41 | +You may refresh a once prop from the client-side using a [partial reload](/guide/partial-reloads). The server will always resolve a once prop when explicitly requested. |
| 42 | + |
| 43 | +:::tabs key:frameworks |
| 44 | +== Vue |
| 45 | + |
| 46 | +```js |
| 47 | +import { router } from '@inertiajs/vue3' |
| 48 | + |
| 49 | +router.reload({ only: ['plans'] }) |
| 50 | +``` |
| 51 | + |
| 52 | +== React |
| 53 | + |
| 54 | +```js |
| 55 | +import { router } from '@inertiajs/react' |
| 56 | + |
| 57 | +router.reload({ only: ['plans'] }) |
| 58 | +``` |
| 59 | + |
| 60 | +== Svelte 4|Svelte 5 |
| 61 | + |
| 62 | +```js |
| 63 | +import { router } from '@inertiajs/svelte' |
| 64 | + |
| 65 | +router.reload({ only: ['plans'] }) |
| 66 | +``` |
| 67 | + |
| 68 | +::: |
| 69 | + |
| 70 | +## Expiration |
| 71 | + |
| 72 | +You may set an expiration time using the `expires_in` parameter. This parameter accepts a `Time`, `DateTime`, `ActiveSupport::Duration`, or an integer (seconds). The prop will be refreshed on a subsequent visit after the expiration time has passed. |
| 73 | + |
| 74 | +```ruby |
| 75 | +class DashboardController < ApplicationController |
| 76 | + def index |
| 77 | + render inertia: { |
| 78 | + plans: InertiaRails.once(expires_in: 1.day) { Plan.all }, |
| 79 | + } |
| 80 | + end |
| 81 | +end |
| 82 | +``` |
| 83 | + |
| 84 | +## Custom Keys |
| 85 | + |
| 86 | +You may assign a custom key to the prop using the `key` parameter. This is useful when you want to share data across multiple pages while using different prop names. |
| 87 | + |
| 88 | +```ruby |
| 89 | +class TeamsController < ApplicationController |
| 90 | + def index |
| 91 | + render inertia: { |
| 92 | + memberRoles: InertiaRails.once(key: 'roles') { Role.all }, |
| 93 | + } |
| 94 | + end |
| 95 | + |
| 96 | + def invite |
| 97 | + render inertia: { |
| 98 | + availableRoles: InertiaRails.once(key: 'roles') { Role.all }, |
| 99 | + } |
| 100 | + end |
| 101 | +end |
| 102 | +``` |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +## Sharing Once Props |
| 107 | + |
| 108 | +You may share once props globally using the `inertia_share` controller method. |
| 109 | + |
| 110 | +```ruby |
| 111 | +class ApplicationController < ActionController::Base |
| 112 | + inertia_share countries: InertiaRails.once { Country.all } |
| 113 | +end |
| 114 | +``` |
| 115 | + |
| 116 | +## Prefetching |
| 117 | + |
| 118 | +Once props are compatible with [prefetching](/guide/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. |
| 119 | + |
| 120 | +Prefetched pages containing an expired once prop will be invalidated from the cache. |
| 121 | + |
| 122 | +## Combining with Other Prop Types |
| 123 | + |
| 124 | +The `once()` modifier may be chained onto [deferred](/guide/deferred-props), [merge](/guide/merging-props), and [optional](/guide/partial-reloads#lazy-data-evaluation) props. |
| 125 | + |
| 126 | +```ruby |
| 127 | +class DashboardController < ApplicationController |
| 128 | + def index |
| 129 | + render inertia: { |
| 130 | + memberRoles: InertiaRails.once(key: 'roles') { Role.all }, |
| 131 | + permissions: InertiaRails.defer(once: true) { Permission.all }, |
| 132 | + activity: InertiaRails.merge(once: true) { @user.recent_activity }, |
| 133 | + categories: InertiaRails.optional(once: true) { Category.all }, |
| 134 | + } |
| 135 | + end |
| 136 | +end |
| 137 | +``` |
0 commit comments