Looking for advice on optimising images with Astro, content collections, and MDX
astro:assets doesn't export ImageMetadata, making it hard to pass images as props.
astrojs - Import an image from an Astro.props variable - Stack Overflow
hey anyone knows how to fix astro Image component type error
Videos
I've been building a blog/portfolio site using Astro with content collections and MDX. I'm trying to optimise images inside blog posts. Each post has multiple images, and I use a custom Astro component to display them with detailed captions in MDX files.
The issue is that Astro’s built-in Image component only works with statically imported images. That doesn’t play well with content collections and MDX, where importing each image manually isn’t practical, especially when posts have lots of images or grouped images with text blocks. Something would be limiting with just markdown syntax.
From what I’ve found, the dynamic import method using import.meta.glob() works, but it requires all images to live in a single folder. I’d prefer keeping assets organized inside each content folder (e.g., separate assets for blog/post vs. work), which that approach doesn’t really support.
The only other option seems to be putting everything in /public and using external tools for optimisation, but that feels like a workaround.
Has anyone faced this issue? How are you guys handling image optimisation in Astro with MDX and content collections?
» npm install @astrojs/image
Hello Everyone getting this error on build time using astro image component and cover is image fetched from content matter
export const posts = defineCollection({
type: 'content',
schema: ({ image }) =>
z.object({
title: z.string().max(80),
description: z.string(),
pubDate: z
.string()
.or(z.date())
.transform((val) => new Date(val)),
cover: image(),
coverAlt: z.string(),
coverImgSourceName: z.string(),
coverImgSourceLink: z.string(),
category: z.enum(CATEGORIES),
keywords: z.array(z.string()),
draft: z.boolean().default(false),
tableOfContents: z.array(z.string()),
relatedPosts: z.array(z.string())
})
})