Skip to content

Commit

Permalink
Content collections - fix .json collection errors (#7246)
Browse files Browse the repository at this point in the history
* fix: avoid error on collectionType === 'unknown'

* fix: ignore underscores in file globs

* chore: clarify [!_]

* fix: mismatch error not throwing

* fix: bad collectionType var

* test: no error for empty collection

* chore: changeset
  • Loading branch information
bholmesdev committed May 30, 2023
1 parent 8da07ab commit f5063d0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-rabbits-rhyme.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix content collection build errors for empty collections or underscore files of type `.json`.
18 changes: 13 additions & 5 deletions packages/astro/src/content/types-generator.ts
Expand Up @@ -414,7 +414,11 @@ async function writeContentFiles({
for (const collectionKey of Object.keys(collectionEntryMap).sort()) {
const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)];
const collection = collectionEntryMap[collectionKey];
if (collectionConfig?.type && collection.type !== collectionConfig.type) {
if (
collectionConfig?.type &&
collection.type !== 'unknown' &&
collection.type !== collectionConfig.type
) {
viteServer.ws.send({
type: 'error',
err: new AstroError({
Expand All @@ -433,7 +437,14 @@ async function writeContentFiles({
});
return;
}
switch (collection.type) {
const resolvedType: 'content' | 'data' =
collection.type === 'unknown'
? // Add empty / unknown collections to the data type map by default
// This ensures `getCollection('empty-collection')` doesn't raise a type error
collectionConfig?.type ?? 'data'
: collection.type;

switch (resolvedType) {
case 'content':
contentTypesStr += `${collectionKey}: {\n`;
for (const entryKey of Object.keys(collection.entries).sort()) {
Expand All @@ -449,9 +460,6 @@ async function writeContentFiles({
contentTypesStr += `};\n`;
break;
case 'data':
// Add empty / unknown collections to the data type map by default
// This ensures `getCollection('empty-collection')` doesn't raise a type error
case 'unknown':
dataTypesStr += `${collectionKey}: {\n`;
for (const entryKey of Object.keys(collection.entries).sort()) {
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
Expand Down
8 changes: 6 additions & 2 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Expand Up @@ -43,8 +43,12 @@ export function astroContentVirtualModPlugin({
new URL('reference-map.json', contentPaths.cacheDir).pathname
)
.replace('@@CONTENT_DIR@@', relContentDir)
.replace('@@CONTENT_ENTRY_GLOB_PATH@@', `${relContentDir}**/*${getExtGlob(contentEntryExts)}`)
.replace('@@DATA_ENTRY_GLOB_PATH@@', `${relContentDir}**/*${getExtGlob(dataEntryExts)}`)
.replace(
'@@CONTENT_ENTRY_GLOB_PATH@@',
// [!_] = ignore files starting with "_"
`${relContentDir}**/[!_]*${getExtGlob(contentEntryExts)}`
)
.replace('@@DATA_ENTRY_GLOB_PATH@@', `${relContentDir}**/[!_]*${getExtGlob(dataEntryExts)}`)
.replace(
'@@RENDER_ENTRY_GLOB_PATH@@',
`${relContentDir}**/*${getExtGlob(/** Note: data collections excluded */ contentEntryExts)}`
Expand Down
Expand Up @@ -119,4 +119,32 @@ title: Post
expect(e.hint).to.include("Try adding `type: 'data'`");
}
});

it('does not raise error for empty collection with config', async () => {
const fs = createFsWithFallback(
{
// Add placeholder to ensure directory exists
'/src/content/i18n/_placeholder.txt': 'Need content here',
'/src/content/config.ts': `
import { z, defineCollection } from 'astro:content';
const i18n = defineCollection({
type: 'data',
schema: z.object({
greeting: z.string(),
}),
});
export const collections = { i18n };`,
},
root
);

try {
const res = await sync({ fs });
expect(res).to.equal(0);
} catch (e) {
expect.fail(0, 1, `Did not expect sync to throw: ${e.message}`);
}
});
});

0 comments on commit f5063d0

Please sign in to comment.