From 56099c1e3fd917a08441165186e8c9b54bbe4b0a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 28 Mar 2023 11:37:41 -0400 Subject: [PATCH] fix: correct empty string to '/' for index route --- packages/astro/src/core/render/paginate.ts | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/astro/src/core/render/paginate.ts b/packages/astro/src/core/render/paginate.ts index 62526eb97f3c..dffabe178269 100644 --- a/packages/astro/src/core/render/paginate.ts +++ b/packages/astro/src/core/render/paginate.ts @@ -39,6 +39,21 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio ...additionalParams, [paramName]: includesFirstPageNumber || pageNum > 1 ? String(pageNum) : undefined, }; + const current = correctIndexRoute(routeMatch.generate({ ...params })); + const next = + pageNum === lastPage + ? undefined + : correctIndexRoute(routeMatch.generate({ ...params, page: String(pageNum + 1) })); + const prev = + pageNum === 1 + ? undefined + : correctIndexRoute( + routeMatch.generate({ + ...params, + page: + !includesFirstPageNumber && pageNum - 1 === 1 ? undefined : String(pageNum - 1), + }) + ); return { params, props: { @@ -51,23 +66,7 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio total: data.length, currentPage: pageNum, lastPage: lastPage, - url: { - current: routeMatch.generate({ ...params }), - next: - pageNum === lastPage - ? undefined - : routeMatch.generate({ ...params, page: String(pageNum + 1) }), - prev: - pageNum === 1 - ? undefined - : routeMatch.generate({ - ...params, - page: - !includesFirstPageNumber && pageNum - 1 === 1 - ? undefined - : String(pageNum - 1), - }), - }, + url: { current, next, prev }, } as Page, }, }; @@ -75,3 +74,13 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio return result; }; } + +function correctIndexRoute(route: string) { + // `routeMatch.generate` avoids appending `/` + // unless `trailingSlash: 'always'` is configured. + // This means an empty string is possible for the index route. + if (route === '') { + return '/'; + } + return route; +}