In this episode, we're diving into a practical implementation of infinite scrolling using Inertia's latest features. We'll start by setting up the groundwork: creating an Article
model, alongside its migration, factory, and seeder. We'll then seed our database with a bunch of articles, making sure each has an incrementing date, so it's easy to see our infinite scrolling in action later.
Once our data is ready, we set up an ArticlesController
and a corresponding Inertia page to display our articles. Initially, we just dump all thousand articles to the page – which is obviously not ideal – so we quickly pivot to paginating them.
From there, we explore how we used to handle infinite scrolling: by reloading and merging data manually. But with Inertia's new prop merging and the whenVisible
component, it's much easier now! We show how to send only the new articles for each page and let Inertia handle the merging behind the scenes.
First, we wire up a simple "Load More" button so you can trigger fetching additional articles one page at a time. Then, we take it up a notch by swapping in the whenVisible
component, which lets us trigger loading more articles automatically as you scroll near the bottom of the list—boom, infinite scroll without much fuss!
We also discuss how to tell the user when they've reached the end of the list and show a little trick to clean up your seeder when you want to regenerate test data. The main takeaway: with these new tools in Inertia, you can build infinite scrolling with very little custom code.
So, by the end of this episode, you'll have a smooth infinite scroll experience set up for your articles (or anything else), and you'll see just how little manual work is required now. Super handy until Inertia releases an official infinite scroll component!