Skip to content

Commit 0e1c1fa

Browse files
hxrshxzdoradocodes
authored andcommitted
Fix TutorialLayout crash for tutorials without optional fields
1 parent 7d87115 commit 0e1c1fa

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/content/tutorials/config.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,37 @@ export const categories = [
1919
export const tutorialsCollection = defineCollection({
2020
type: "content",
2121
schema: ({ image }) =>
22-
z.object({
23-
// Title of the tutorial
24-
title: z.string(),
25-
// People who wrote the tutorial
26-
authors: z.array(z.string()).optional(),
27-
// Optional note explaining more context about the authors
28-
authorsNote: z.string().optional(),
29-
description: z.string().optional(),
30-
category: z.enum(categories),
31-
categoryIndex: z.number().optional(),
32-
// Image to use as a thumbnail for the tutorial
33-
featuredImage: image().optional(),
34-
featuredImageAlt: z.string().optional(),
35-
relatedContent: relatedContent().optional(),
36-
}),
22+
z
23+
.object({
24+
// Title of the tutorial
25+
title: z.string(),
26+
// People who wrote the tutorial
27+
authors: z.array(z.string()).optional(),
28+
// Optional note explaining more context about the authors
29+
authorsNote: z.string().optional(),
30+
description: z.string().optional(),
31+
category: z.enum(categories),
32+
categoryIndex: z.number().optional(),
33+
// Image to use as a thumbnail for the tutorial
34+
featuredImage: image().optional(),
35+
featuredImageAlt: z.string().optional(),
36+
relatedContent: relatedContent().optional(),
37+
})
38+
.refine(
39+
(data) => {
40+
// Allow missing fields during development for quicker iteration
41+
if (import.meta.env.DEV) return true;
42+
return (
43+
data.authors !== undefined &&
44+
data.authors.length > 0 &&
45+
data.featuredImage !== undefined &&
46+
data.featuredImageAlt !== undefined &&
47+
data.description !== undefined
48+
);
49+
},
50+
{
51+
message:
52+
"Tutorials must include authors, featuredImage, featuredImageAlt, and description for production builds",
53+
},
54+
),
3755
});

src/layouts/TutorialLayout.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ const relatedExamples =
5050
<Head
5151
title={entry.data.title}
5252
locale={currentLocale}
53-
description={entry.data.authors.join(", ")}
54-
featuredImageSrc={entry.data.featuredImage.src}
53+
description={entry.data.authors?.join(", ")}
54+
featuredImageSrc={entry.data.featuredImage?.src}
5555
/>
5656

5757
<BaseLayout

0 commit comments

Comments
 (0)