inertia-server, working with pages is based on the concept
of page definitions created by the definePage function.
Creating page definitions
Page definitions consist of the component name, and a set of props that will be passed to it.~/server/inertia.ts
render function. It is created by invoking your page definition with the required props.
Data & Props
Page props builders -prop, mergedProp, deepMergedProp provide various chained methods
to help you customize your data loading behavior.
Merging props
Inertia overwrites props with the same name when reloading a page. However, you may need to merge new data with existing data instead. For example, when implementing a “load more” button for paginated results. Prop merging only works during partial reloads. Full page reloads will override the props with the new values.prepend method.
matchOn option.
By sending items with the same key, elements will be replaced, and rest - appended or prepended.
Deep merging
Instead of specifying which nested paths should be merged, you may usedeepMergedProp to ensure a deep merge of the entire structure.
Deep merging was introduced before normal merging had support for prepending and targeting nested paths. In most cases,
mergedProp()
with matchOn and its append and prepend methods should be sufficient.Infinite scrolling
Merged props can be used to implement infinite scrolling with inertia<InfiniteScroll> component.
page query parameter from your request and use it as the page number. To customise it, you can use the pageName option.
hasMore prop. It will be used to determine if there is more data to load.
Lazy loading
When rendering a page, everyprop value can be a literal, or a lazy evaluated function.
It’s up to you which data loading strategy to use. Let’s see how it works on an example.
foo prop will be sent during every page request, but if you define
its value as a function, it will be resolved when inertia needs to parse its value,
not at the moment of the render call. It is especially useful when you work with partial
data requests.
Interestingly, the bar prop will not be parsed by inertia on the first request. By default
you should provide a lazy evaluable function to defer execution of your code until it’s needed.
But if you want to, you can provide a literal value at the moment of the render call. It will
not be sent to the client, but it may useful if you want to execute some side effect when a page
is rendered and the prop prepared, no matter whether its loaded or not.
Deferred props
To defer a prop, you can use thedeferred() method on the prop definition.
teams and roles props will be fetched in one request, while the permissions prop will be fetched in a separate request in parallel. Group names are arbitrary strings and can be anything you choose.
On the client side, inertia provides the Deferred component to handle deferred props. It is not required with inertia-server, because we correctly type deferred props as optional, so you can
just check for the value and show a loading state. If you need to
differentiate between the loading state and a potential empty value, use the component though.
Prop modifiers
When Inertia performs a request, it will determine which data is required and only then will it evaluate the closure. With prop modifiers, you can manually control this behavior. Useoptional() method to specify that a prop should never be included unless explicitly requested using the only client option.
On the reverse, you can use always() method to specify that a prop should always be included, even in partial reloads.
To load a prop on the first request, and then to keep it in the cache, you can use once() method.
Accessing other page object properties
Flash data and errors are available inside the props object. To access page url, version and other metadata, use hooks from your framework of choice.react