Manage Page Props and Context
masterInertia props can be shared globally via the inertiaManager or contextually via the request context. Contextual props are added using WithProp methods and then applied to the request using r.WithContext(ctx).
Prop Types and Evaluation
| Method | Evaluation | Full | Partial |
|---|---|---|---|
Share, WithProp, Render | Eager | ✅ | ✅ if requested |
WithOptionalProp | Lazy | ❌ | ✅ if requested |
WithAlwaysProp | Lazy | ✅ | ✅ always |
WithDeferredProp | Lazy | ❌ deferred | ✅ if requested |
WithMergeProp | Lazy | ✅ | ✅ if requested |
WithDeepMergeProp | Lazy | ✅ | ✅ if requested |
WithPrependProp | Lazy | ✅ | ✅ if requested |
WithScrollProp | — | ✅ metadata | ✅ if requested |
WithOnceProp, WithOnce | Lazy | ✅ | ✅ if requested |
WithErrorProp | Eager | ✅ always | ✅ always |
Key Concepts
- Deferred Props: Use
WithDeferredPropfor data that should be loaded asynchronously. UseWithRescuedDeferredPropto catch errors and move the key torescuedPropsinstead of failing. - Merging: Use
WithMergeProporWithDeepMergePropto combine new data with existing props. You can specify match keys (e.g.,id) or nested paths (e.g.,users.data.id). - Once Props: Use
WithOnceorWithOncePropto ensure data is only sent once. These are excluded by theX-Inertia-Except-Once-Propsheader. - Scroll Props: Use
WithScrollPropto handle pagination metadata. It should be paired with a merge or prepend prop.
// Example: Contextual Deferred Prop
ctx := inertiaManager.WithDeferredProp(r.Context(), "comments", func() any {
return getComments()
})
r = r.WithContext(ctx)
// Example: Contextual Merge Prop with match key
ctx := inertiaManager.WithMergeProp(r.Context(), "results", func() any {
return getResults()
}, "id")
r = r.WithContext(ctx)