From 3f3e4f12863910fc86cc1dfb0adb68635a8a512f Mon Sep 17 00:00:00 2001 From: "Tony @ Navillus" <60468564+tony-navillus@users.noreply.github.com> Date: Wed, 23 Jun 2021 12:40:58 +0000 Subject: [PATCH] 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 e8a976a543070bce4d661b5289e16d1097a593bc. * Adding changeset --- .changeset/shy-houses-do.md | 5 ++ docs/collections.md | 6 +- packages/astro/src/runtime.ts | 2 +- packages/astro/test/astro-collection.test.js | 55 +++++++++++++++++++ .../astro-collection/src/pages/$grouped.astro | 30 ++++++++++ .../src/pages/$individual.astro | 29 ++++++++++ .../src/pages/post/nested/a.md | 1 + .../astro-collection/src/pages/post/one.md | 1 + .../astro-collection/src/pages/post/three.md | 1 + .../astro-collection/src/pages/post/two.md | 1 + 10 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 .changeset/shy-houses-do.md create mode 100644 packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro create mode 100644 packages/astro/test/fixtures/astro-collection/src/pages/$individual.astro diff --git a/.changeset/shy-houses-do.md b/.changeset/shy-houses-do.md new file mode 100644 index 0000000000..d5cd6d16c5 --- /dev/null +++ b/.changeset/shy-houses-do.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow `pageSize: Infinity` when creating a collection diff --git a/docs/collections.md b/docs/collections.md index 0f714c308b..474bbe7a16 100644 --- a/docs/collections.md +++ b/docs/collections.md @@ -137,7 +137,7 @@ export async function createCollection() { // 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 // disable pagination. - pageSize: 1, + pageSize: Infinity, }; } --- @@ -179,8 +179,8 @@ export async function createCollection() { return allPokemon[params.index]; }, // 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. - pageSize: 1, + // is ever returned per route. We set it to Infinity in this example for completeness. + pageSize: Infinity, }; } --- diff --git a/packages/astro/src/runtime.ts b/packages/astro/src/runtime.ts index 1b86b4ec87..db3940b921 100644 --- a/packages/astro/src/runtime.ts +++ b/packages/astro/src/runtime.ts @@ -153,7 +153,7 @@ async function load(config: RuntimeConfig, rawPathname: string | undefined): Pro // paginate 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); collection.start = start; diff --git a/packages/astro/test/astro-collection.test.js b/packages/astro/test/astro-collection.test.js index a1825dc43e..058ebbbb65 100644 --- a/packages/astro/test/astro-collection.test.js +++ b/packages/astro/test/astro-collection.test.js @@ -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(); diff --git a/packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro b/packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro new file mode 100644 index 0000000000..0bcae6b6b8 --- /dev/null +++ b/packages/astro/test/fixtures/astro-collection/src/pages/$grouped.astro @@ -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 + }; +} +--- + +