diff --git a/.changeset/hungry-dancers-hug.md b/.changeset/hungry-dancers-hug.md new file mode 100644 index 000000000000..43b994ef0c4b --- /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 7b8654ee7037..3309bc642aea 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; type GlobResult = Record; @@ -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 e39d7da5e7fc..bc0e90d2aa87 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 000000000000..a811df0717ff --- /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 000000000000..991994433870 --- /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 000000000000..4b5e0ffb8631 --- /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) +--- + +

This should still render

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1aebf6930f4c..77a91fb1584b 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: