Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content collections - fix .json collection errors #7246

Merged
merged 7 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-rabbits-rhyme.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}`);
}
});
});