In this episode, we dive into one of the handy new features in Inertia: the WhenVisible
component. If you've ever wrestled with the Intersection Observer API, or set up complicated watchers to only load data when a certain part of your page comes into view, you'll appreciate how much this simplifies things!
We start by simulating some fake user and article data, showing how you might output this data inside containers on your dashboard. Then we set up a scenario where you wouldn't want to load data for some articles until the user actually scrolls down to see them — perfect for improving performance, especially when you have slow API requests.
Previously, you would've had to manually wire up Intersection Observer or use a library like VueUse, listening for the component to come into view and then triggering a reload. Now, all you need is to mark your data as "optional" (using the new inertia.optional
helper), and wrap the relevant section in the new WhenVisible
component. As soon as that part of your UI appears, the data loads — with a fallback for a loading message while data is on the way.
We also explore some extra configuration, like setting a buffer (which lets you start loading before the component is fully in view), and toggling the always
prop to reload data every time the component comes into view (great for admin panels needing always-fresh data without polling).
By the end, you'll see that with just a small amount of markup, you can easily defer loading data until it's actually needed — no need to bolt on extra JavaScript, just leverage that lovely WhenVisible
wrapper from Inertia.