Georg Ledermann PRO said 11 months ago on Infinite Scrolling with Hotwire :
The first drawback of the no-JS approach can be mitigated by this code in the controller:

class PostsController < ApplicationController
  ...  
  def index    
    respond_to do |format|
      format.html
      format.turbo_stream { @pagy, @posts = pagy_countless(Post.all) }    
    end  
  end
  ...

It ensures that posts are loaded for turbo_stream requests only.

David Kimura PRO said 11 months ago on Infinite Scrolling with Hotwire :
Good point. I think if I were to avoid the initial query, I would leave the post index action blank and have the turbo frame tag call an entirely different route. That should work as well.

dev said 10 months ago on Infinite Scrolling with Hotwire :
To prevent endless, looping from the last element of the collection to the first element
(Approach 2 - no-JS)

<%= turbo_stream.append "posts" do %>
   <%= render partial: "posts/post", collection: @posts %>
 <% end %>
<% unless @pagy.page == @pagy.last %>
  <%= turbo_stream.replace "pagination" do %>
    <%= turbo_frame_tag "pagination", src: posts_path(page: @pagy.next, format: :turbo_stream), loading: :lazy %>
  <% end %>
<% end %>


To reset the infinite list and clear the page params:
(Approach 1 - Stimulus Controller)

  <%= link_to 'Posts', root_path(only_path: true), class: 'text-decoration-none text-reset fs-2' %>

Login to Comment