Skip to content

Commit

Permalink
fix: correct empty string to '/' for index route
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Mar 28, 2023
1 parent 0f2db9d commit 56099c1
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions packages/astro/src/core/render/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -51,27 +66,21 @@ 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,
},
};
});
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;
}

0 comments on commit 56099c1

Please sign in to comment.