summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Dan Jutan <danjutan@gmail.com> 2023-02-08 19:14:47 -0500
committerGravatar GitHub <noreply@github.com> 2023-02-08 19:14:47 -0500
commit436bd09341693fc705f2a55d460eed3afa413432 (patch)
tree0ff9c55925141c2b974da60f26a62250af41aa2c
parenta64a800d9bc6c31eb22b4c00441c420b19fd9ebe (diff)
downloadastro-436bd09341693fc705f2a55d460eed3afa413432.tar.gz
astro-436bd09341693fc705f2a55d460eed3afa413432.tar.zst
astro-436bd09341693fc705f2a55d460eed3afa413432.zip
Fix root directory spread pagination `url.prev` for first page (#6183)
* Fix root pagination `url.prev` for first page * fix lockfile? * add changeset
-rw-r--r--.changeset/calm-readers-learn.md5
-rw-r--r--packages/astro/src/core/render/paginate.ts4
-rw-r--r--packages/astro/test/astro-pagination-root-spread.test.js33
-rw-r--r--packages/astro/test/fixtures/astro-pagination-root-spread/package.json8
-rw-r--r--packages/astro/test/fixtures/astro-pagination-root-spread/src/pages/[...page].astro16
-rw-r--r--pnpm-lock.yaml6
6 files changed, 69 insertions, 3 deletions
diff --git a/.changeset/calm-readers-learn.md b/.changeset/calm-readers-learn.md
new file mode 100644
index 000000000..63c5c8922
--- /dev/null
+++ b/.changeset/calm-readers-learn.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes the first-page value of `url.prev` when paginating a spread route at the root
diff --git a/packages/astro/src/core/render/paginate.ts b/packages/astro/src/core/render/paginate.ts
index ddcca066f..799637e0f 100644
--- a/packages/astro/src/core/render/paginate.ts
+++ b/packages/astro/src/core/render/paginate.ts
@@ -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,
diff --git a/packages/astro/test/astro-pagination-root-spread.test.js b/packages/astro/test/astro-pagination-root-spread.test.js
new file mode 100644
index 000000000..4b96a6790
--- /dev/null
+++ b/packages/astro/test/astro-pagination-root-spread.test.js
@@ -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);
+ })
+ );
+ });
+});
diff --git a/packages/astro/test/fixtures/astro-pagination-root-spread/package.json b/packages/astro/test/fixtures/astro-pagination-root-spread/package.json
new file mode 100644
index 000000000..f542c7d34
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-pagination-root-spread/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "@test/astro-pagination-root",
+ "version": "0.0.0",
+ "private": true,
+ "dependencies": {
+ "astro": "workspace:*"
+ }
+}
diff --git a/packages/astro/test/fixtures/astro-pagination-root-spread/src/pages/[...page].astro b/packages/astro/test/fixtures/astro-pagination-root-spread/src/pages/[...page].astro
new file mode 100644
index 000000000..9d097febf
--- /dev/null
+++ b/packages/astro/test/fixtures/astro-pagination-root-spread/src/pages/[...page].astro
@@ -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>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3e1a40bf7..8b59a9e56 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1482,6 +1482,12 @@ importers:
dependencies:
astro: link:../../..
+ packages/astro/test/fixtures/astro-pagination-root-spread:
+ specifiers:
+ astro: workspace:*
+ dependencies:
+ astro: link:../../..
+
packages/astro/test/fixtures/astro-partial-html:
specifiers:
'@astrojs/react': workspace:*