summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/hungry-dancers-hug.md5
-rw-r--r--packages/astro/src/content/runtime.ts19
-rw-r--r--packages/astro/test/content-collections.test.js16
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-dir/package.json9
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts11
-rw-r--r--packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro8
-rw-r--r--pnpm-lock.yaml6
7 files changed, 70 insertions, 4 deletions
diff --git a/.changeset/hungry-dancers-hug.md b/.changeset/hungry-dancers-hug.md
new file mode 100644
index 000000000..43b994ef0
--- /dev/null
+++ b/.changeset/hungry-dancers-hug.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Do not throw an error for an empty collection directory.
diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts
index 7b8654ee7..3309bc642 100644
--- a/packages/astro/src/content/runtime.ts
+++ b/packages/astro/src/content/runtime.ts
@@ -13,6 +13,7 @@ import {
type AstroComponentFactory,
} from '../runtime/server/index.js';
import type { ContentLookupMap } from './utils.js';
+import type { AstroIntegration } from '../@types/astro.js';
type LazyImport = () => Promise<any>;
type GlobResult = Record<string, LazyImport>;
@@ -55,10 +56,7 @@ export function createGetCollection({
} else if (collection in dataCollectionToEntryMap) {
type = 'data';
} else {
- throw new AstroError({
- ...AstroErrorData.CollectionDoesNotExistError,
- message: AstroErrorData.CollectionDoesNotExistError.message(collection),
- });
+ return warnOfEmptyCollection(collection)
}
const lazyImports = Object.values(
type === 'content'
@@ -392,3 +390,16 @@ type PropagatedAssetsModule = {
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
return typeof module === 'object' && module != null && '__astroPropagation' in module;
}
+
+function warnOfEmptyCollection(collection: string): AstroIntegration {
+ return {
+ name: 'astro-collection',
+ hooks: {
+ 'astro:server:start': ({ logger }) => {
+ logger.warn(
+ `The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.`
+ );
+ },
+ },
+ };
+}
diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js
index e39d7da5e..bc0e90d2a 100644
--- a/packages/astro/test/content-collections.test.js
+++ b/packages/astro/test/content-collections.test.js
@@ -244,6 +244,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;
diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/package.json b/packages/astro/test/fixtures/content-collections-empty-dir/package.json
new file mode 100644
index 000000000..a811df071
--- /dev/null
+++ b/packages/astro/test/fixtures/content-collections-empty-dir/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "@test/content-collections-empty-dir",
+ "version": "0.0.0",
+ "private": true,
+ "type": "module",
+ "dependencies": {
+ "astro": "workspace:*"
+ }
+}
diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts
new file mode 100644
index 000000000..991994433
--- /dev/null
+++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts
@@ -0,0 +1,11 @@
+import { z, defineCollection } from 'astro:content';
+
+const blog = defineCollection({
+ schema: z.object({
+ title: z.string(),
+ }),
+});
+
+export const collections = {
+ blog,
+};
diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro
new file mode 100644
index 000000000..4b5e0ffb8
--- /dev/null
+++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro
@@ -0,0 +1,8 @@
+---
+import { getCollection } from 'astro:content'
+
+const blogs = await getCollection("blogs")
+// console.log(blogs)
+---
+
+<p>This should still render</p>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index eb5e684fa..bd09af378 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2367,6 +2367,12 @@ importers:
specifier: workspace:*
version: link:../../..
+ packages/astro/test/fixtures/content-collections-empty-dir:
+ dependencies:
+ astro:
+ specifier: workspace:*
+ version: link:../../..
+
packages/astro/test/fixtures/content-collections-empty-md-file:
dependencies:
astro: