withastro/astro

ImageNotFound error in content collection item when using `image().or(z.string())`

FjellOverflow opened this issue · 0 comments

Astro Info

Astro                    v5.0.3
Node                     v22.9.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind
                         @astrojs/mdx
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

Describe the Bug

When using image().or(z.string()) in a content collection definition, Astro will always throw a ImageNotFound error, when a bare string is given instead of a local asset.

We can define

defineCollection({
  loader: glob({ pattern: '**/[^_]*.md', base: './content/example' }),
  schema: ({ image }) =>
    z.object({
      title: z.string(),
      previewImage: image().or(z.string())
    })
})

and set previewImage to reference some asset in the public directory to skip Astros image optimization.

---
title: Example
previewImage: '/example.png'
---
Example

Now we'll always get [ERROR] [ImageNotFound] Could not find requested image '/example.png'. Does it exist?

What's the expected result?

In Astro version 4.x.y I could use image().or(z.string()) to allow both referencing local assets, optimized by Astro and a bare string to give some public or external image url.

In Astro version 5, it will always expect an existing image and throw error if a mere string is given.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-2q7gtw-wnxkkr?file=src%2Fcontent.config.ts

Participation

  • I am willing to submit a pull request for this issue.