Paginating large data sets is a common use case in web applications and APIs. Supercharge integrates a Pagination utility providing convenient access to paginate query results and also a pagination HTML view.


At first, import the Pagination utility from the Supercharge framework. Creating a new Pagination instance accepts an options object with three properties:

  1. request (required): the request object that includes a query parameter named page with an integer value
  2. totalCount (required): the total amount of entries in your collection
  3. perPage (optional): the amount of items you want to display per page; the default value is 8 items per page

Pagination in Supercharge requires you to have a query parameter called page on your request to determine the current page. Pagination based on path parameters doesn’t work at this point.

Here’s a code snippet illustrating pagination:

const Paginator = require('@supercharge/framework/pagination')

module.exports = {
  handler: async (request, h) => {
    const totalCount = await Movie.count()
    const pagination = new Paginator({ request, totalCount, perPage: 12 })

    if (pagination.currentPage > pagination.lastPage) {
      return h.view('404').code(404)

    const movies = await Movie.find()

    return h.view('movies/index', {

Depending on whether you paginate data sets in a web app or an API, your response is different. You must handle situations yourself where a user requests a page that exceeds the last page of the paginated collection. Supercharge will not throw an error or assign the last page’s value to the requested page.


Imagine a request to The returned object from new Paginator({ request, totalCount }) may look like this:

  total: 26,
  perPage: 8,
  currentPage: 2,
  lastPage: 4,
  first: '',
  prev: '',
  next: '',
  last: '',
  from: 8,
  to: 16,
  link: '<>; rel="first", <>; rel="prev", <>; rel="next", <>; rel="last"'

Displaying Results

Every Supercharge application ships with a pagination Handlebars partial view. This is located in your resources/views/partials directory. This view uses HTML markup that is compatible with the Bootstrap CSS framework.

Use the pagination partial view if you want to display paginated results. Import this partial in your views:

  <!-- paginated data set -->

  {{> pagination}}

Please notice that the pagination partial view requires a pagination object in the response context. The code snippet in the “Usage” section returns a view and adds the pagination details to the view context’s data.

Feel free to update this view in case you’re using another CSS framework or want a different styling.