Feature Request: Excluded props
mochetts opened this issue · comments
It would be ideal to tell inertia rails that when a partial load is done (e.g only: ["some_prop"]
) only that prop is computed. In order to do this, we need an auxiliary method that allows us to skip props on partial loads.
For example, if we have:
inertia_share do
{
heavy_prop: SomethingHeavyToCompute.get,
notifications: NotificationsSerializer.one(current_user)
}
end
And then in the frontend we do:
router.reload({ only: ["notifications"] })
heavy_prop
is not rendered, but it is computed. This is not ideal when we want to work with partial loads.
The only way I can think this can be achieved is by making heavy props excluded on partial loads:
inertia_share do
{
heavy_prop: InertiaRails.on_full_load(-> { SomethingHeavyToCompute.get }),
notifications: NotificationsSerializer.one(current_user)
}
end
Is this something appealing for anyone else? If so, I can work on it and send a PR.
Found out that we can use heavy_prop: -> { SomethingHeavyToCompute.get }
to achieve this. Closing.
Hey @mochetts ! You are correct that inertia-rails supports this feature. Inertia.js calls it partial reloads: https://inertiajs.com/partial-reloads
A few additional nuances:
- If you want to exclude
heavy_prop
on the initial page load, you'll need to wrap the lambda like so:
InertiaRails.lazy(->() { # won't run on initial page load })
- If you want to exclude the other props from running in the partial reload, you'll need to wrap them a lambda. Consider this example:
inertia_share do
{
heavy_prop_1: FirstHeavyThing.run,
heavy_prop_2: InertiaRails.lazy(->() { SecondHeavyThing.run }),
}
end
Even if you only grab heavy_prop_2
on a partial reload, FirstHeavyThing.run
will still run. InertiaRails just won't pass the result back to the frontend. To prevent it from running on every page load, this will work:
inertia_share do
{
heavy_prop_1: ->() { FirstHeavyThing.run }, # now this won't run on partial reloads that don't ask for it
heavy_prop_2: InertiaRails.lazy(->() { SecondHeavyThing.run }),
}
end