#

# JavaScript


# 1. Choose the format that matches your app's configuration

ES6 module
pagy.mjs 5.41KB
Minified IIFE file
pagy.min.js 2.59KB
Plain IIFE file
pagy.js 5.48KB
Source map
pagy.js.map 15.3KB
TypeScript types
pagy.d.ts 106B

# 2. Make the file available to your app

Depending on your app's architecture, you have a couple of options. Just pick one to uncomment in the pagy.rb initializer:

  • For apps with an assets pipeline...
    • Compatible with Propshaft, Importmaps, Sprockets, and similar tools.
    Rails.application.config.assets.paths << Pagy::ROOT.join('javascripts')
  • For apps with a builder...
    • This works with builders like esbuild, Webpack, jsbundling-rails, etc.
    # Example for Rails
    javascript_dir = Rails.root.join('app/javascripts')
    Pagy.sync_javascript(javascript_dir, 'pagy.mjs') if Rails.env.development?
  • Load it like any other JavaScript file or module you already use in your app

# 3. Set up the Pagy.init listener

The primary purpose of the JavaScript support is to utilize Pagy.init as an event listener attached to a suitable window event:

// Plain JavaScript
window.addEventListener("load", Pagy.init)

// Turbo
window.addEventListener("turbo:load", Pagy.init)

// Turbolinks
window.addEventListener("turbolinks:load", Pagy.init)

// Custom event
window.addEventListener("yourEvent", Pagy.init)