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

Ensure index pages are generated on paginated results #4426

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/odd-elephants-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Ensure index pages are generated on paginated results
5 changes: 0 additions & 5 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,6 @@ async function getPathsForRoute(
paths = result.staticPaths
.map((staticPath) => staticPath.params && route.generate(staticPath.params))
.filter((staticPath) => {
// Remove empty or undefined paths
if (!staticPath) {
return false;
}

// The path hasn't been built yet, include it
if (!builtPaths.has(removeTrailingForwardSlash(staticPath))) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/get-static-paths-pages",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
export async function getStaticPaths({ paginate }) {
const astronautPages = [{
astronaut: 'Neil Armstrong',
}, {
astronaut: 'Buzz Aldrin',
}, {
astronaut: 'Sally Ride',
}, {
astronaut: 'John Glenn',
}];
// Generate pages from our array of astronauts, with 2 to a page
return paginate(astronautPages, { pageSize: 2 });
}

// All paginated data is passed on the "page" prop
const { page } = Astro.props;
---

<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>

<body>
<h1>Page {page.currentPage}</h1>
<ul>
<!--List the array of astronaut info-->
{page.data.map(({ astronaut }: { astronaut: string }) => <li>{astronaut}</li>)}
</ul>
</body>

</html>
27 changes: 27 additions & 0 deletions packages/astro/test/get-static-paths-pages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('getStaticPaths with trailingSlash: ignore', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/get-static-paths-pages/',
site: 'https://mysite.dev/',
});
await fixture.build();
});

it('includes index page', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
expect($('h1').text()).to.equal('Page 1');
});

it('includes paginated page', async () => {
let html = await fixture.readFile('/2/index.html');
let $ = cheerio.load(html);
expect($('h1').text()).to.equal('Page 2');
});
});
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

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