Skip to main content
The search() function includes built-in pagination support. Results are sliced server-side (or client-side) and returned with metadata about total results, pages, and current position.

Overview

Pagination is controlled by the PaginationOptions parameter passed to search():

PaginationOptions type

From src/types/index.ts:62-65:
Fields:
  • page?: number - Page number (1-indexed, defaults to 1)
  • limit?: number - Results per page (defaults to 20)
Both fields are optional. If omitted, defaults are applied: { page: 1, limit: 20 }

Default behavior

From src/core/search.ts:309:
When no pagination is provided:
  • Returns first 20 results
  • Current page is 1
  • Total pages calculated based on total results

Pagination metadata

The SearchResponse includes detailed pagination metadata:
From src/types/index.ts:67-76:

Implementation details

From src/core/search.ts:366-374:
1

Normalize page and limit

Both values are clamped to minimum of 1:
2

Calculate offset

Convert page number to array offset (0-indexed):
3

Slice results

Extract the requested page from all sorted results:
4

Calculate metadata

Determine total pages and include in response:

Usage examples

First page (default)

Custom page size

Get all results

Requesting very large page sizes (e.g., limit: 10000) may impact performance. Consider using reasonable page sizes (10-100) for better UX and performance.

Building pagination UI

React example

Page number buttons

Edge cases

Invalid page numbers

Check if currentPage > totalPages to detect out-of-range requests and redirect to the last valid page.

Zero or negative limit

No results

Performance considerations

Pagination happens after search and scoring. All results are computed first, then sliced. For very large result sets, consider implementing server-side pagination if performance becomes an issue.

Search flow

  1. Search runs across all verses (6,236 verses)
  2. Results are scored and sorted
  3. Then pagination slices the sorted results
  4. Only the requested page is returned
This means:
  • ✓ Consistent scoring and ordering across pages
  • ✓ Accurate total counts and page numbers
  • ✗ All results must be computed even if only viewing page 1

Optimization tips