From d7502ee9044452bbf4943269a0a8049f0d8b0784 Mon Sep 17 00:00:00 2001 From: Takeo Sawada Date: Wed, 17 Jul 2024 22:55:53 +0800 Subject: [PATCH] feat: add first and last to the Page interface (#11176) * feat: add first and last to the Page interface * Update .changeset/twenty-maps-glow.md * Update .changeset/twenty-maps-glow.md Co-authored-by: Sarah Rainsberger --------- Co-authored-by: Bjorn Lu Co-authored-by: Matthew Phillips Co-authored-by: Sarah Rainsberger --- .changeset/twenty-maps-glow.md | 5 +++++ packages/astro/src/@types/astro.ts | 4 ++++ packages/astro/src/core/render/paginate.ts | 15 ++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/twenty-maps-glow.md 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, }, };