Fix TutorialLayout crash for tutorials without optional fields#1142
Fix TutorialLayout crash for tutorials without optional fields#1142hxrshxz wants to merge 3 commits intoprocessing:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes crashes in TutorialLayout when tutorials lack optional authors and featuredImage fields by adding optional chaining, and adds build-time validation to ensure production builds require these fields plus description and featuredImageAlt. The solution allows flexible development iterations while enforcing completeness for production deployments.
Changes:
- Added
.refine()validation in tutorial schema that skips validation in dev mode but enforces required fields (authors,featuredImage,featuredImageAlt,description) in production builds - Added optional chaining (
?.) in TutorialLayout forauthorsandfeaturedImagefield accesses to prevent runtime crashes during development
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/content/tutorials/config.ts | Added production-only validation using .refine() to enforce required fields while keeping them optional in the type system for dev flexibility |
| src/layouts/TutorialLayout.astro | Added optional chaining to prevent TypeError crashes when accessing authors and featuredImage properties |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d3a3d9c to
67f3734
Compare
|
I have addressed your feedbacks , thank you |
6d3a661 to
d5ab512
Compare
|
@hxrshxz, @Nwakaego-Ego and I have taken a look at your changes and it looks good. Please update your branch with the latest changes from |
|
@doradocodes Done ! thanks 😄 |
Resolves #1006
The
TutorialLayoutcomponent crashes when a tutorial doesn't havefeaturedImageorauthorsin its frontmatter because it tries to call.srcand.join()on undefined values.These fields are marked optional in the schema but are actually required for the site to function. As discussed in the issue, I went with a
.refine()approach so that:npm run dev, the validation is skipped (viaimport.meta.env.DEV) so you can work on tutorials without filling in every field upfrontnpm run build/npm run check, it enforces thatauthors,featuredImage,featuredImageAlt, anddescriptionare present — so incomplete tutorials can't make it to productionAlso added optional chaining in
TutorialLayout.astroon the two lines that were actually crashing, so the dev server doesn't blow up when those fields are missing.