#
Javascript Setup
Notice
A javascript setup is required only for the pagy*_js
helpers. Just using something like anchor_string: 'data-remote="true"'
in any instances works out of the box with any helper and without this setup.
We don't publish a npm
package, because it would not support automatic sync with the gem version.
Add the oj
gem to your gemfile for faster performance.
#
How does it work?
All the pagy*_js
helpers render their component on the client side. The helper methods render just a minimal HTML tag that
contains a data-pagy
attribute.
Your app should Pagy.init()
data-pagy
attribute data and make it work
in the browser.
#
1. Pick a Javascript File
pagy.mjs
Your app uses modern build tools
- ES6 module to use with webpacker, esbuild, parcel, etc.
module_path = Pagy.root.join('javascripts', 'pagy.mjs')
types_path = Pagy.root.join('javascripts', 'pagy.d.ts')
pagy.min.js
Your app needs standard script or old browser compatibility
- It's an IIFE file meant to be loaded as is, directly in your production pages and without any further processing
- Minified (~2k) and polyfilled to work also with quite old browsers
script_path = Pagy.root.join('javascripts', 'pagy.min.js')
pagy.min.js.map
You need to debug the javascript helpers while using the pagy.min.js
file
script_path = Pagy.root.join('javascripts', 'pagy.min.js.map')
#
2. Configure
Depending on your environment you have a few ways of configuring your app:
#
Rails with assets pipeline
In older versions of Rails, you can configure the app to look into the installed pagy gem javascript files:
Rails.application.config.assets.paths << Pagy.root.join('javascripts') # uncomment.
//= require pagy
Rails.application.config.assets.paths << Pagy.root.join('javascripts') #uncomment
//= link pagy.mjs
pin 'pagy'
Rails.application.config.assets.paths << Pagy.root.join('javascripts')
<%= javascript_include_tag "pagy" %>
#
Builders
In order to bundle the pagy.mjs
your builder has to find it either with a link or local copy, or by looking into the pagy
javascript path:
You can create a symlink or a copy of the pagy.mjs
file (available in the pagy gem) into an app compiled dir and use it as
a regular app file. That way any builder will pick it up. For example:
# Create/refresh the `app/javascript/pagy.mjs` symlink/copy every time
# the app restarts (unless in production), ensuring syncing when pagy is updated.
# Replace the FileUtils.ln_sf with FileUtils.cp if your OS doesn't support file linking.
FileUtils.ln_sf(Pagy.root.join('javascripts', 'pagy.mjs'), Rails.root.join('app', 'javascript')) \
unless Rails.env.production?
Prepend the NODE_PATH
environment variable to the scripts.build
command:
{
"build": "NODE_PATH=\"$(bundle show 'pagy')/javascripts\" <your original command>"
}
Prepend the NODE_PATH
environment variable to the scripts.build
command:
{
"build": "NODE_PATH=\"$(bundle show 'pagy')/javascripts\" <your original command>"
}
module.exports = {
..., // your original config
resolve: { // add resolve.modules
modules: [
"node_modules", // node_modules dir
process.env.PAGY_PATH // pagy dir
]
}
}
#
Non-Rails apps
- Just ensure
Pagy.root.join('javascripts', 'pagy.js')
is served with the page.
#
3. Initialize Pagy
After the helper is loaded you have to initialize Pagy
to make it work:
import {Controller} from "@hotwired/stimulus"
import Pagy from "pagy" // if using sprockets, you can remove above line, but make sure you have the appropriate directive if your manifest.js file.
export default class extends Controller {
connect() {
Pagy.init(this.element)
}
}
<div data-controller="pagy-initializer">
<%== pagy_nav_js(@pagy) %>
</div>
Import and use the pagy module:
import Pagy from "pagy";
window.addEventListener("turbo:load", Pagy.init);
// if you choose pagy.mjs
import Pagy from "pagy"
// plain javascript
window.addEventListener("load", Pagy.init)
// Turbo
window.addEventListener("turbo:load", Pagy.init)
// Turbolinks
window.addEventListener("turbolinks:load", Pagy.init)
// custom listener
window.addEventListener(yourEventListener, Pagy.init)
#
Caveats
HTML Fallback
If Javascript is disabled in the client browser, certain helpers will be useless. Consider implementing your own HTML fallback:
<noscript><%== pagy_nav(@pagy) %></noscript>
Overriding *_js
helpers is not recommended
The pagy*_js
helpers are tightly coupled with the javascript code, so any partial overriding on one side, would be quite fragile
and might break in a next release.