Skip to content

Commit de60c69

Browse files
authoredJun 14, 2024··
Fix a performance issue with JSON schema generation (#11249)
1 parent 773828a commit de60c69

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed
 

‎.changeset/early-spies-bow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a performance issue with JSON schema generation

‎packages/astro/src/content/types-generator.ts

+34-33
Original file line numberDiff line numberDiff line change
@@ -449,41 +449,42 @@ async function writeContentFiles({
449449
for (const entryKey of Object.keys(collection.entries).sort()) {
450450
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
451451
dataTypesStr += `${entryKey}: {\n id: ${entryKey};\n collection: ${collectionKey};\n data: ${dataType}\n};\n`;
452-
if (
453-
settings.config.experimental.contentCollectionJsonSchema &&
454-
collectionConfig?.schema
455-
) {
456-
let zodSchemaForJson =
457-
typeof collectionConfig.schema === 'function'
458-
? collectionConfig.schema({ image: () => z.string() })
459-
: collectionConfig.schema;
460-
if (zodSchemaForJson instanceof z.ZodObject) {
461-
zodSchemaForJson = zodSchemaForJson.extend({
462-
$schema: z.string().optional(),
463-
});
464-
}
465-
try {
466-
await fs.promises.writeFile(
467-
new URL(`./${collectionKey.replace(/"/g, '')}.schema.json`, collectionSchemasDir),
468-
JSON.stringify(
469-
zodToJsonSchema(zodSchemaForJson, {
470-
name: collectionKey.replace(/"/g, ''),
471-
markdownDescription: true,
472-
errorMessages: true,
473-
}),
474-
null,
475-
2
476-
)
477-
);
478-
} catch (err) {
479-
logger.warn(
480-
'content',
481-
`An error was encountered while creating the JSON schema for the ${entryKey} entry in ${collectionKey} collection. Proceeding without it. Error: ${err}`
482-
);
483-
}
452+
dataTypesStr += `};\n`;
453+
}
454+
455+
if (
456+
settings.config.experimental.contentCollectionJsonSchema &&
457+
collectionConfig?.schema
458+
) {
459+
let zodSchemaForJson =
460+
typeof collectionConfig.schema === 'function'
461+
? collectionConfig.schema({ image: () => z.string() })
462+
: collectionConfig.schema;
463+
if (zodSchemaForJson instanceof z.ZodObject) {
464+
zodSchemaForJson = zodSchemaForJson.extend({
465+
$schema: z.string().optional(),
466+
});
467+
}
468+
try {
469+
await fs.promises.writeFile(
470+
new URL(`./${collectionKey.replace(/"/g, '')}.schema.json`, collectionSchemasDir),
471+
JSON.stringify(
472+
zodToJsonSchema(zodSchemaForJson, {
473+
name: collectionKey.replace(/"/g, ''),
474+
markdownDescription: true,
475+
errorMessages: true,
476+
}),
477+
null,
478+
2
479+
)
480+
);
481+
} catch (err) {
482+
logger.warn(
483+
'content',
484+
`An error was encountered while creating the JSON schema for the ${collectionKey} collection. Proceeding without it. Error: ${err}`
485+
);
484486
}
485487
}
486-
dataTypesStr += `};\n`;
487488
break;
488489
}
489490
}

0 commit comments

Comments
 (0)
Please sign in to comment.