fix(route/twreporter): Prevent crash when hero_image or og_image is missing (#20481)

The original code directly accessed nested properties on
`post.hero_image`, which is not guaranteed to exist.
This cause a TypeError when the image object was nil.

This commit introduces safe access by:
- Prioritizing `hero_image` but falling back to `og_image`
- Only rendering the banner if a valid image URL was resolved
This commit is contained in:
IChengHo 2025-11-13 22:22:13 -05:00 committed by GitHub
parent 2ae10a7a02
commit 6969cf1bcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 3 deletions

View File

@ -30,11 +30,14 @@ export default async function fetch(slug: string) {
authors += '' + photographers;
}
const bannerImage = post.hero_image.resized_targets.desktop.url;
// Prioritize hero_image, but fall back to og_image if it's missing
const imageSource = post.hero_image ?? post.og_image;
const bannerImage = imageSource?.resized_targets.desktop.url;
const caption = post.leading_image_description;
const bannerDescription = post.hero_image.description;
const bannerDescription = imageSource?.description ?? '';
const ogDescription = post.og_description;
const banner = art(path.join(__dirname, 'templates/image.art'), { image: bannerImage, description: bannerDescription, caption });
// Only render the banner if we successfully found an image URL
const banner = imageSource ? art(path.join(__dirname, 'templates/image.art'), { image: bannerImage, description: bannerDescription, caption }) : '';
function format(type, content) {
let block = '';