Returns a Pagination object with prev and next entries based on the flat list of page nodes in tree order. Either can be null if the current page is at the start or end of the list.

import { getNodePagination } from 'slugtree'

const pagination = getNodePagination(['guides', 'routing'])
// { prev: PaginationItem | null, next: PaginationItem | null }

Parameters

ParameterTypeDefaultDescription
slugstring | string[][]Slug of the current page.

Return value

Returns Pagination | null. Returns null if the slug is not found in the page list.

interface Pagination {
  prev: PaginationItem | null
  next: PaginationItem | null
}

interface PaginationItem {
  title: string
  description?: string
  href: string
  slug: string[]
}

Usage

import { getNodePagination } from 'slugtree'

export default function PageFooter({ slug }: { slug: string[] }) {
  const pagination = getNodePagination(slug)
  if (!pagination) return null

  return (
    <footer>
      {pagination.prev && (
        <a href={pagination.prev.href}>← {pagination.prev.title}</a>
      )}
      {pagination.next && (
        <a href={pagination.next.href}>{pagination.next.title} →</a>
      )}
    </footer>
  )
}

For client components, use useNodePagination() from slugtree/react.