Skip to content

Conversation

@skryukov
Copy link
Contributor

@skryukov skryukov commented Oct 16, 2025

This PR introduces infinite scroll functionality to Inertia Rails. It comes with out of the box support for Pagy & Kaminari gems and ability to register custom scroll metadata adapters.

Pagy

class UsersController < ApplicationController
  include Pagy::Backend

  def index
    pagy, records = pagy(User.all)

    render inertia: {
      users: InertiaRails.scroll(pagy) { records.as_json(...) }
    }
  end
end

Kaminari

class UsersController < ApplicationController
  def index
    users = User.page(params[:page])

    render inertia: {
      # Pass collection to the scroll method to extract pagination metadata
      users: InertiaRails.scroll(users) { users.as_json(...) },
    }
  end
end

Manual

class UsersController < ApplicationController
  def index
    meta, users = paginate(User.order(:name))

    render inertia: {
      users: InertiaRails.scroll(meta) { users.as_json(...) }
    }
  end

  private

  PER_PAGE = 20

  def paginate(scope, page_param: :page)
    page = [params.fetch(page_param, 1).to_i, 1].max

    records = scope.offset((page - 1) * PER_PAGE).limit(PER_PAGE + 1)

    meta = {
      page_name: page_param.to_s,
      previous_page: page > 1 ? page - 1 : nil,
      next_page: records.length > PER_PAGE ? page + 1 : nil,
      current_page: page
    }

    [meta, records.first(PER_PAGE)]
  end
end

For more info see the provided docs.

@skryukov skryukov force-pushed the infinite-scroll branch 3 times, most recently from 1785e3b to fdf742d Compare October 17, 2025 14:35
@skryukov skryukov marked this pull request as ready for review October 17, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant