Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Paginate adds unexpected trailing slash on index route #6676

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-humans-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix next and previous links for index routes when using pagination
41 changes: 26 additions & 15 deletions packages/astro/src/core/render/paginate.ts
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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,25 +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 ? '' : 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;
}
22 changes: 19 additions & 3 deletions packages/astro/test/astro-pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ describe('Pagination', () => {
{ color: 'blue', p: '2' },
];
await Promise.all(
params.map(async ({ color, p }) => {
params.map(async ({ color, p }, idx) => {
const html = await fixture.readFile(`/posts/${color}/${p}/index.html`);
const $ = cheerio.load(html);
expect($('#page-a').text()).to.equal(p);
expect($('#page-b').text()).to.equal(p);
expect($('#page-param').text()).to.equal(p);
expect($('#currentPage').text()).to.equal(p);
expect($('#filter').text()).to.equal(color);

const prevHref = $('#prev').attr('href');
const nextHref = $('#next').attr('href');

if (color === 'red') {
expect(prevHref).to.be.undefined;
expect(nextHref).to.be.undefined;
}
if (color === 'blue' && p === 1) {
expect(prevHref).to.be.undefined;
expect(nextHref).to.equal('/posts/blue/2');
}
if (color === 'blue' && p === 2) {
expect(prevHref).to.equal('/posts/blue/1');
expect(nextHref).to.be.undefined;
Copy link
Member

@MoustaphaDev MoustaphaDev Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests every run? Say when p = '1' ===> p === 1 is false which will cause the test to not run

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this is a super imperative way to check if the params entry matches one of the items in the list (see the lines just above). Brain-dead solution I know, but oddly the simplest I could find 😅

Copy link
Member

@MoustaphaDev MoustaphaDev Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that p is always a string so comparing it to the numbers will return false
'1' !=== 1

I'm I overlooking something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhhh right you are! I'm a fool. Will fix

}
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
<link rel="canonical" href={canonicalURL.href} />
</head>
<body>
<div id="page-a">{params.page}</div>
<div id="page-b">{page.currentPage}</div>
<div id="page-param">{params.page}</div>
<div id="currentPage">{page.currentPage}</div>
<div id="filter">{filter}</div>
<a href={page.url.prev} id="prev">Previous</a>
<a href={page.url.next} id="next">Next</a>
</body>
</html>