Skip to content

Commit

Permalink
Fix an issue where getCollection might return undefined (#10356)
Browse files Browse the repository at this point in the history
* Fix an issue where `getCollection` might return `undefined`

* Add changeset
  • Loading branch information
mingjunlu authored Mar 7, 2024
1 parent f973aa9 commit d121311
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-bats-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where `getCollection` might return `undefined` when content collection is empty
2 changes: 1 addition & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function createGetCollection({
collection
)} does not exist or is empty. Ensure a collection directory with this name exists.`
);
return;
return [];
}
const lazyImports = Object.values(
type === 'content'
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('Content Collections', () => {
});

describe('With empty collections directory', () => {
it('Handles the empty directory correclty', async () => {
it('Handles the empty directory correctly', async () => {
const fixture = await loadFixture({
root: './fixtures/content-collections-empty-dir/',
});
Expand All @@ -272,7 +272,12 @@ describe('Content Collections', () => {
error = e.message;
}
assert.equal(error, undefined);
// TODO: try to render a page

const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const h1 = $('h1');
assert.equal(h1.text(), 'Entries length: 0');
assert.equal(h1.attr('data-entries'), '[]');
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
import { getCollection } from 'astro:content'
const blogs = await getCollection("blogs")
// console.log(blogs)
const entries = await getCollection('blogs');
---

<p>This should still render</p>
<h1 data-entries={JSON.stringify(entries)}>Entries length: {entries.length}</h1>

0 comments on commit d121311

Please sign in to comment.