Update createCollection() to handle pageSize: Infinity
(#516)
* Fix pageSize calculation when Infinity is given
* test grouping collection with pageSize: Infinity
* test individual pages for collection items
* Revert "update docs, remove reference to Inifinity"
This reverts commit e8a976a543
.
* Adding changeset
This commit is contained in:
parent
28c0a45f3c
commit
3f3e4f1286
10 changed files with 127 additions and 4 deletions
5
.changeset/shy-houses-do.md
Normal file
5
.changeset/shy-houses-do.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Allow `pageSize: Infinity` when creating a collection
|
|
@ -137,7 +137,7 @@ export async function createCollection() {
|
||||||
// Finally, `pageSize` and `pagination` is still on by default. Because
|
// Finally, `pageSize` and `pagination` is still on by default. Because
|
||||||
// we don't want to paginate the already-grouped pages a second time, we'll
|
// we don't want to paginate the already-grouped pages a second time, we'll
|
||||||
// disable pagination.
|
// disable pagination.
|
||||||
pageSize: 1,
|
pageSize: Infinity,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
@ -179,8 +179,8 @@ export async function createCollection() {
|
||||||
return allPokemon[params.index];
|
return allPokemon[params.index];
|
||||||
},
|
},
|
||||||
// Note: The default pageSize is fine because technically only one data object
|
// Note: The default pageSize is fine because technically only one data object
|
||||||
// is ever returned per route. We can set it to "1" in this example for completeness.
|
// is ever returned per route. We set it to Infinity in this example for completeness.
|
||||||
pageSize: 1,
|
pageSize: Infinity,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
|
|
@ -153,7 +153,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro
|
||||||
|
|
||||||
// paginate
|
// paginate
|
||||||
if (searchResult.currentPage) {
|
if (searchResult.currentPage) {
|
||||||
const start = (searchResult.currentPage - 1) * pageSize; // currentPage is 1-indexed
|
const start = pageSize === Infinity ? 0 : (searchResult.currentPage - 1) * pageSize; // currentPage is 1-indexed
|
||||||
const end = Math.min(start + pageSize, data.length);
|
const end = Math.min(start + pageSize, data.length);
|
||||||
|
|
||||||
collection.start = start;
|
collection.start = start;
|
||||||
|
|
|
@ -54,4 +54,59 @@ Collections('can load remote data', async ({ runtime }) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Collections('generates pages grouped by author', async ({ runtime }) => {
|
||||||
|
const AUTHORS_TO_TEST = [
|
||||||
|
{
|
||||||
|
id: 'author-one',
|
||||||
|
posts: ['one', 'three'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'author-two',
|
||||||
|
posts: ['two'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'author-three',
|
||||||
|
posts: ['nested/a'],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const { id, posts } of AUTHORS_TO_TEST) {
|
||||||
|
const result = await runtime.load(`/grouped/${id}`);
|
||||||
|
if (result.error) throw new Error(result.error);
|
||||||
|
const $ = doc(result.contents);
|
||||||
|
|
||||||
|
assert.ok($(`#${id}`).length);
|
||||||
|
|
||||||
|
for (const post of posts) {
|
||||||
|
assert.ok($(`a[href="/post/${post}"]`).length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Collections('generates individual pages from a collection', async ({ runtime }) => {
|
||||||
|
const PAGES_TO_TEST = [
|
||||||
|
{
|
||||||
|
slug: 'one',
|
||||||
|
title: 'Post One',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'two',
|
||||||
|
title: 'Post Two',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'three',
|
||||||
|
title: 'Post Three',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const { slug, title } of PAGES_TO_TEST) {
|
||||||
|
const result = await runtime.load(`/individual/${slug}`);
|
||||||
|
if (result.error) throw new Error(result.error);
|
||||||
|
const $ = doc(result.contents);
|
||||||
|
|
||||||
|
assert.ok($(`#${slug}`).length);
|
||||||
|
assert.equal($(`h1`).text(), title);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Collections.run();
|
Collections.run();
|
||||||
|
|
30
packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro
vendored
Normal file
30
packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro
vendored
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
---
|
||||||
|
export let collection: any;
|
||||||
|
|
||||||
|
export async function createCollection() {
|
||||||
|
const allPosts = Astro.fetchContent('./post/**/*.md');
|
||||||
|
const allAuthors = allPosts.map(p => p.author);
|
||||||
|
const uniqueAuthors = [...new Set(allAuthors)];
|
||||||
|
|
||||||
|
return {
|
||||||
|
routes: uniqueAuthors.map(author => {
|
||||||
|
const params = { name: author };
|
||||||
|
return params;
|
||||||
|
}),
|
||||||
|
|
||||||
|
permalink: ({ params }) => `/grouped/${params.name}`,
|
||||||
|
|
||||||
|
async data({ params }) {
|
||||||
|
return allPosts.filter(p => p.author === params.name)
|
||||||
|
},
|
||||||
|
|
||||||
|
pageSize: Infinity
|
||||||
|
};
|
||||||
|
}
|
||||||
|
---
|
||||||
|
|
||||||
|
<div id={collection.params.name}>
|
||||||
|
{collection.data.map((post) => (
|
||||||
|
<a href={post.url}>{post.title}</a>
|
||||||
|
))}
|
||||||
|
</div>
|
29
packages/astro/test/fixtures/astro-collection/src/pages/$individual.astro
vendored
Normal file
29
packages/astro/test/fixtures/astro-collection/src/pages/$individual.astro
vendored
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
export let collection: any;
|
||||||
|
|
||||||
|
export async function createCollection() {
|
||||||
|
const allPosts = Astro.fetchContent('./post/*.md');
|
||||||
|
|
||||||
|
return {
|
||||||
|
routes: allPosts.map((post, i) => {
|
||||||
|
const params = {
|
||||||
|
slug: post.url.replace('/post/', ''),
|
||||||
|
index: i
|
||||||
|
};
|
||||||
|
return params;
|
||||||
|
}),
|
||||||
|
|
||||||
|
permalink: ({ params }) => `/individual/${params.slug}`,
|
||||||
|
|
||||||
|
async data({ params }) {
|
||||||
|
return [allPosts[params.index]];
|
||||||
|
},
|
||||||
|
|
||||||
|
pageSize: Infinity
|
||||||
|
};
|
||||||
|
}
|
||||||
|
---
|
||||||
|
|
||||||
|
<div id={collection.params.slug}>
|
||||||
|
<h1>{collection.data[0].title}</h1>
|
||||||
|
</div>
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
title: Post A
|
title: Post A
|
||||||
date: 2021-04-16 00:00:00
|
date: 2021-04-16 00:00:00
|
||||||
|
author: author-three
|
||||||
---
|
---
|
||||||
|
|
||||||
# Post A
|
# Post A
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
title: Post One
|
title: Post One
|
||||||
date: 2021-04-13 00:00:00
|
date: 2021-04-13 00:00:00
|
||||||
|
author: author-one
|
||||||
---
|
---
|
||||||
|
|
||||||
# Post One
|
# Post One
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
title: Post Three
|
title: Post Three
|
||||||
date: 2021-04-15 00:00:00
|
date: 2021-04-15 00:00:00
|
||||||
|
author: author-one
|
||||||
---
|
---
|
||||||
|
|
||||||
# Post Three
|
# Post Three
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
title: Post Two
|
title: Post Two
|
||||||
date: 2021-04-14 00:00:00
|
date: 2021-04-14 00:00:00
|
||||||
|
author: author-two
|
||||||
---
|
---
|
||||||
|
|
||||||
# Post Two
|
# Post Two
|
||||||
|
|
Loading…
Add table
Reference in a new issue