From 436bd09341693fc705f2a55d460eed3afa413432 Mon Sep 17 00:00:00 2001 From: Dan Jutan Date: Wed, 8 Feb 2023 19:14:47 -0500 Subject: [PATCH] Fix root directory spread pagination `url.prev` for first page (#6183) * Fix root pagination `url.prev` for first page * fix lockfile? * add changeset --- .changeset/calm-readers-learn.md | 5 +++ packages/astro/src/core/render/paginate.ts | 4 +-- .../test/astro-pagination-root-spread.test.js | 33 +++++++++++++++++++ .../astro-pagination-root-spread/package.json | 8 +++++ .../src/pages/[...page].astro | 16 +++++++++ pnpm-lock.yaml | 6 ++++ 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .changeset/calm-readers-learn.md create mode 100644 packages/astro/test/astro-pagination-root-spread.test.js create mode 100644 packages/astro/test/fixtures/astro-pagination-root-spread/package.json create mode 100644 packages/astro/test/fixtures/astro-pagination-root-spread/src/pages/[...page].astro 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; +--- + 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:*