Skip to content

Commit

Permalink
Remove AstroError if content directory is empty (#8382)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
  • Loading branch information
DerTimonius and ematipico committed Sep 21, 2023
1 parent 4398e92 commit e522a5e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
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
---

Do not throw an error for an empty collection directory.
19 changes: 15 additions & 4 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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.`
);
},
},
};
}
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 @@ -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;

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.

0 comments on commit e522a5e

Please sign in to comment.