diff --git a/.changeset/twenty-maps-glow.md b/.changeset/twenty-maps-glow.md new file mode 100644 index 0000000000..9588a45bc2 --- /dev/null +++ b/.changeset/twenty-maps-glow.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Adds two new values to the [pagination `page` prop](https://docs.astro.build/en/reference/api-reference/#the-pagination-page-prop): `page.first` and `page.last` for accessing the URLs of the first and last pages. diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 015966db42..aac7b4dcb0 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -2647,6 +2647,10 @@ export interface Page { prev: string | undefined; /** url of the next page (if there is one) */ next: string | undefined; + /** url of the first page (if the current page is not the first page) */ + first: string | undefined; + /** url of the next page (if the current page in not the last page) */ + last: string | undefined; }; } diff --git a/packages/astro/src/core/render/paginate.ts b/packages/astro/src/core/render/paginate.ts index 00aba3702b..ab6654e5c6 100644 --- a/packages/astro/src/core/render/paginate.ts +++ b/packages/astro/src/core/render/paginate.ts @@ -56,6 +56,19 @@ export function generatePaginateFunction( !includesFirstPageNumber && pageNum - 1 === 1 ? undefined : String(pageNum - 1), }) ); + const first = + pageNum === 1 + ? undefined + : correctIndexRoute( + routeMatch.generate({ + ...params, + page: includesFirstPageNumber? "1": undefined, + }) + ); + const last = + pageNum === lastPage + ? undefined + : correctIndexRoute(routeMatch.generate({ ...params, page: String(lastPage)})); return { params, props: { @@ -68,7 +81,7 @@ export function generatePaginateFunction( total: data.length, currentPage: pageNum, lastPage: lastPage, - url: { current, next, prev }, + url: { current, next, prev, first, last }, } as Page, }, };