Fix root directory spread pagination url.prev for first page (#6183)

* Fix root pagination `url.prev` for first page

* fix lockfile?

* add changeset
This commit is contained in:
Dan Jutan 2023-02-08 19:14:47 -05:00 committed by GitHub
parent a64a800d9b
commit 436bd09341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes the first-page value of `url.prev` when paginating a spread route at the root

View file

@ -63,9 +63,7 @@ export function generatePaginateFunction(routeMatch: RouteData): PaginateFunctio
: routeMatch.generate({ : routeMatch.generate({
...params, ...params,
page: page:
!includesFirstPageNumber && pageNum - 1 === 1 !includesFirstPageNumber && pageNum - 1 === 1 ? '' : String(pageNum - 1),
? undefined
: String(pageNum - 1),
}), }),
}, },
} as Page, } as Page,

View file

@ -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);
})
);
});
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/astro-pagination-root",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -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>

View file

@ -1482,6 +1482,12 @@ importers:
dependencies: dependencies:
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/astro-pagination-root-spread:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/astro-partial-html: packages/astro/test/fixtures/astro-partial-html:
specifiers: specifiers:
'@astrojs/react': workspace:* '@astrojs/react': workspace:*