Deviopath › Next.js

Page Number From searchParams

Easy · Search Params

A page receives the query string as the searchParams prop. In Next.js 16 it is a Promise, so you write const { page } = await searchParams. Each value can be a string, a string[] (when the key appears more than once, like ?page=2&page=5) or undefined.

Write getPage(searchParams) that takes the already-awaited object and returns the page number:

- If page is an array, use its first item.

- If the value is a whole number greater than 0, return it as a number.

- Otherwise (missing, "0", negative, not a number) return 1.

Next: robots.ts