My content collection allows you to include images. I'd like to make them optional and provide a default fallback, but when i try, i get an error when building. However, it does work in dev mode, it only breaks upon building.
Any idea how to make it work without re-architecturing things? I don't want to rethink my organization to make it work.
my content.config.ts, simplified for length:
import { z, defineCollection } from 'astro:content';
import { glob, file } from 'astro/loaders';
// Using @content syntax in the import is also broken
import defaultDemonsCategorySculpture from './content/demons/nexus/default-sculpture.webp'
const demonsCategoriesCollection = defineCollection({
loader: file("./src/content/demons/categories.yaml"),
schema: ({ image }) => z.object({
name: z.string(),
sculptureSrc: image().optional(),
}).transform((data) => ({
...data,
sculptureSrc: data.sculptureSrc || defaultDemonsCategorySculpture,
}))
});
Example data from categories.yaml:
- id: "dark-souls-3"
name: "Dark Souls III"
sculptureSrc: "@content/demons/dark-souls-3/sculpture.webp"
How i use the images:
<Image src={category.data.sculptureSrc} alt="" width="256" height="256" loading="eager" />
The error i get:
14:41:16 ▶ /_astro/bg.B11gS7d1_ZkfShr.webp (reused cache entry) (+9ms) (311/654)
node:internal/fs/promises:641
return new FileHandle(await PromisePrototypeThen(
^
Error: ENOENT: no such file or directory, open '/home/aria/aria-dog/dist/@fs/home/aria/aria-dog/src/content/demons/nexus/default-sculpture.webp'