summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Mark Gaze <mark.l.gaze@gmail.com> 2024-06-14 11:38:20 +0100
committerGravatar GitHub <noreply@github.com> 2024-06-14 11:38:20 +0100
commitde60c69aa06c41f76a5510cc1d0bee4c8a5326a5 (patch)
tree8737de24b4799566e7622bfea44992f882b0d324
parent773828a0562d48ec9604323b8cd5f87cf6800200 (diff)
downloadastro-de60c69aa06c41f76a5510cc1d0bee4c8a5326a5.tar.gz
astro-de60c69aa06c41f76a5510cc1d0bee4c8a5326a5.tar.zst
astro-de60c69aa06c41f76a5510cc1d0bee4c8a5326a5.zip
Fix a performance issue with JSON schema generation (#11249)
-rw-r--r--.changeset/early-spies-bow.md5
-rw-r--r--packages/astro/src/content/types-generator.ts67
2 files changed, 39 insertions, 33 deletions
diff --git a/.changeset/early-spies-bow.md b/.changeset/early-spies-bow.md
new file mode 100644
index 000000000..8141484cc
--- /dev/null
+++ b/.changeset/early-spies-bow.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes a performance issue with JSON schema generation
diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts
index 31c1e67e7..f1ce6edfb 100644
--- a/packages/astro/src/content/types-generator.ts
+++ b/packages/astro/src/content/types-generator.ts
@@ -449,41 +449,42 @@ async function writeContentFiles({
for (const entryKey of Object.keys(collection.entries).sort()) {
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
dataTypesStr += `${entryKey}: {\n id: ${entryKey};\n collection: ${collectionKey};\n data: ${dataType}\n};\n`;
- if (
- settings.config.experimental.contentCollectionJsonSchema &&
- collectionConfig?.schema
- ) {
- let zodSchemaForJson =
- typeof collectionConfig.schema === 'function'
- ? collectionConfig.schema({ image: () => z.string() })
- : collectionConfig.schema;
- if (zodSchemaForJson instanceof z.ZodObject) {
- zodSchemaForJson = zodSchemaForJson.extend({
- $schema: z.string().optional(),
- });
- }
- try {
- await fs.promises.writeFile(
- new URL(`./${collectionKey.replace(/"/g, '')}.schema.json`, collectionSchemasDir),
- JSON.stringify(
- zodToJsonSchema(zodSchemaForJson, {
- name: collectionKey.replace(/"/g, ''),
- markdownDescription: true,
- errorMessages: true,
- }),
- null,
- 2
- )
- );
- } catch (err) {
- logger.warn(
- 'content',
- `An error was encountered while creating the JSON schema for the ${entryKey} entry in ${collectionKey} collection. Proceeding without it. Error: ${err}`
- );
- }
+ dataTypesStr += `};\n`;
+ }
+
+ if (
+ settings.config.experimental.contentCollectionJsonSchema &&
+ collectionConfig?.schema
+ ) {
+ let zodSchemaForJson =
+ typeof collectionConfig.schema === 'function'
+ ? collectionConfig.schema({ image: () => z.string() })
+ : collectionConfig.schema;
+ if (zodSchemaForJson instanceof z.ZodObject) {
+ zodSchemaForJson = zodSchemaForJson.extend({
+ $schema: z.string().optional(),
+ });
+ }
+ try {
+ await fs.promises.writeFile(
+ new URL(`./${collectionKey.replace(/"/g, '')}.schema.json`, collectionSchemasDir),
+ JSON.stringify(
+ zodToJsonSchema(zodSchemaForJson, {
+ name: collectionKey.replace(/"/g, ''),
+ markdownDescription: true,
+ errorMessages: true,
+ }),
+ null,
+ 2
+ )
+ );
+ } catch (err) {
+ logger.warn(
+ 'content',
+ `An error was encountered while creating the JSON schema for the ${collectionKey} collection. Proceeding without it. Error: ${err}`
+ );
}
}
- dataTypesStr += `};\n`;
break;
}
}