Infinite Scroll in Rails with Stimulus.js Discussion
This is super slick. I wonder how easily one could append the query parameter in the URL to reflect the current page? Could
History.replaceState() handle the job?
This rocks, coupled it with my MessagesController for Group Chats to reduce some of the load for the larger chats with lots of messages.
So excited to see this episode. Saw it pop up on your Github a while back, and was excited to see the video.
Thanks @excid3, very neat :-) One comment though, as capturing scroll events like this is usually quite CPU-intensive, wouldn't it be better to use an
Great episode. And a nice introduction to Stimulus.js to boot. One small comment, you may want to wrap the pagination area in a "display: none;" so it doesn't flash on the screen and isn't present when you hit the bottom.
For anyone implementing infinite scrolling behavior, I recommend checking out the method used by https://infinite-scroll.com
Instead of using JSON it uses regular HTML pages like you're already used to. No need to modify the controller or create a separate view template. Instead, they fetch the full
http://localhost:3000/?page=2 (or whatever), but only use the content inside
<div data-target="infinite-scroll.entries"> and append that to the existing div.
I thought it was a clever approach that feels very familiar as it reminds me of Turbolinks. There's very little setup and easy to maintain. With proper caching any performance hit is likely negligible.
I can see the strategy being used with only a slight modification of the code presented in the video.