Fix TutorialLayout crash for tutorials without optional fields#1142
Open
hxrshxz wants to merge 3 commits intoprocessing:mainfrom
Open
Fix TutorialLayout crash for tutorials without optional fields#1142hxrshxz wants to merge 3 commits intoprocessing:mainfrom
hxrshxz 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.
497c286 to
d3a3d9c
Compare
d3a3d9c to
67f3734
Compare
Contributor
Author
|
I have addressed your feedbacks , thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.