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

Remove AstroError if content directory is empty #8382

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/hungry-dancers-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

remove AstroError for empty collection directory
ematipico marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 5 additions & 3 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export function createGetCollection({
} else if (collection in dataCollectionToEntryMap) {
type = 'data';
} else {
throw new AstroError({
...AstroErrorData.CollectionDoesNotExistError,
message: AstroErrorData.CollectionDoesNotExistError.message(collection),
return zodString().transform((_, ctx) => {
ctx.addIssue({
code: ZodIssueCode.custom,
message: `The collection **${collection}** does not exist. Ensure a collection directory with this name exists.`,
});
ematipico marked this conversation as resolved.
Show resolved Hide resolved
});
}
const lazyImports = Object.values(
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ describe('Content Collections', () => {
});
});

describe('With empty collections directory', () => {
it('Handles the empty directory correclty', async () => {
const fixture = await loadFixture({
root: './fixtures/content-collections-empty-dir/'
});
let error;
try {
await fixture.build();
} catch (e) {
error = e.message;
}
expect(error).to.be.undefined;
// TODO: try to render a page
})
})

describe('SSR integration', () => {
let app;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/content-collections-empty-dir",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z, defineCollection } from 'astro:content';

const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
});

export const collections = {
blog,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
import { getCollection } from 'astro:content'

const blogs = await getCollection("blogs")
// console.log(blogs)
---

<p>This should still render</p>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

Loading