Skip to content

Commit

Permalink
Fix root directory spread pagination url.prev for first page (#6183)
Browse files Browse the repository at this point in the history
* Fix root pagination `url.prev` for first page

* fix lockfile?

* add changeset
  • Loading branch information
Dan Jutan authored Feb 9, 2023
1 parent a64a800 commit 436bd09
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-readers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes the first-page value of `url.prev` when paginating a spread route at the root
4 changes: 1 addition & 3 deletions packages/astro/src/core/render/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio
: routeMatch.generate({
...params,
page:
!includesFirstPageNumber && pageNum - 1 === 1
? undefined
: String(pageNum - 1),
!includesFirstPageNumber && pageNum - 1 === 1 ? '' : String(pageNum - 1),
}),
},
} as Page,
Expand Down
33 changes: 33 additions & 0 deletions packages/astro/test/astro-pagination-root-spread.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Pagination root', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-pagination-root-spread/',
site: 'https://mysite.dev/',
base: '/blog',
});
await fixture.build();
});

it('correct prev url in root spread', async () => {
const prevMap = {
'/4/': '/3',
'/3/': '/2',
'/2/': '/',
'/': undefined,
};

await Promise.all(
Object.entries(prevMap).map(async ([curr, prev]) => {
const html = await fixture.readFile(curr + 'index.html');
const $ = cheerio.load(html);
expect($('#prev').attr('href')).to.equal(prev);
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-pagination-root",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
export async function getStaticPaths({ paginate }) {
const astronautPages = [{
astronaut: 'Neil Armstrong',
}, {
astronaut: 'Buzz Aldrin',
}, {
astronaut: 'Sally Ride',
}, {
astronaut: 'John Glenn',
}];
return paginate(astronautPages, { pageSize: 1 });
}
const { page } = Astro.props;
---
<a id="prev" href={page.url.prev}>Back</a>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 436bd09

Please sign in to comment.