Skip to content

Commit e522a5e

Browse files
Remove AstroError if content directory is empty (#8382)
Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 4398e92 commit e522a5e

File tree

7 files changed

+70
-4
lines changed

7 files changed

+70
-4
lines changed

.changeset/hungry-dancers-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Do not throw an error for an empty collection directory.

packages/astro/src/content/runtime.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type AstroComponentFactory,
1414
} from '../runtime/server/index.js';
1515
import type { ContentLookupMap } from './utils.js';
16+
import type { AstroIntegration } from '../@types/astro.js';
1617

1718
type LazyImport = () => Promise<any>;
1819
type GlobResult = Record<string, LazyImport>;
@@ -55,10 +56,7 @@ export function createGetCollection({
5556
} else if (collection in dataCollectionToEntryMap) {
5657
type = 'data';
5758
} else {
58-
throw new AstroError({
59-
...AstroErrorData.CollectionDoesNotExistError,
60-
message: AstroErrorData.CollectionDoesNotExistError.message(collection),
61-
});
59+
return warnOfEmptyCollection(collection)
6260
}
6361
const lazyImports = Object.values(
6462
type === 'content'
@@ -392,3 +390,16 @@ type PropagatedAssetsModule = {
392390
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
393391
return typeof module === 'object' && module != null && '__astroPropagation' in module;
394392
}
393+
394+
function warnOfEmptyCollection(collection: string): AstroIntegration {
395+
return {
396+
name: 'astro-collection',
397+
hooks: {
398+
'astro:server:start': ({ logger }) => {
399+
logger.warn(
400+
`The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.`
401+
);
402+
},
403+
},
404+
};
405+
}

packages/astro/test/content-collections.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ describe('Content Collections', () => {
244244
});
245245
});
246246

247+
describe('With empty collections directory', () => {
248+
it('Handles the empty directory correclty', async () => {
249+
const fixture = await loadFixture({
250+
root: './fixtures/content-collections-empty-dir/'
251+
});
252+
let error;
253+
try {
254+
await fixture.build();
255+
} catch (e) {
256+
error = e.message;
257+
}
258+
expect(error).to.be.undefined;
259+
// TODO: try to render a page
260+
})
261+
})
262+
247263
describe('SSR integration', () => {
248264
let app;
249265

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/content-collections-empty-dir",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"dependencies": {
7+
"astro": "workspace:*"
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z, defineCollection } from 'astro:content';
2+
3+
const blog = defineCollection({
4+
schema: z.object({
5+
title: z.string(),
6+
}),
7+
});
8+
9+
export const collections = {
10+
blog,
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
import { getCollection } from 'astro:content'
3+
4+
const blogs = await getCollection("blogs")
5+
// console.log(blogs)
6+
---
7+
8+
<p>This should still render</p>

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)