#wrong image format
1 messages · Page 1 of 1 (latest)
Hi @wary fractal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro’s <Image /> component does produce .webp by default when it is actually optimizing the image. [Image ref]
However, there are cases where the output will stay as the original format (e.g. .png) instead of being optimized:
- If the image is not a local file in
src/imported as ESM, but instead:- from
public/(e.g.src="/images/foo.png") - or a remote URL
then Astro will not optimize it and the result is effectively the same as a normal<img>. [Images in .astro; Image guide]
- from
To get a .webp output:
-
Make sure
heroAstrideis imported from insidesrc/:--- import { Image } from 'astro:assets'; import heroAstride from '../assets/hero-astride.png'; --- <Image src={heroAstride} alt="Illustration d'une personnage" class="animate-hero-figure h-auto w-full select-none" loading="eager" width={720} /> -
If you want to be explicit, you can set the format:
<Image src={heroAstride} alt="Illustration d'une personnage" width={720} format="webp" />
If heroAstride is already imported from src/ as above and you still see .png, the knowledge sources don’t provide more specific troubleshooting steps, so I can’t say why with certainty.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@wary fractal Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@ancient chasm the image is imported like this, is it a problem?
import heroAstride from "$styles/assets/hero/astride.png"
my tsconfig:
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"$components/": ["src/components/"],
"$layouts/": ["src/layouts/"],
"$content/": ["content/"],
"$styles/": ["src/styles/"],
"$lib/": ["src/lib/"],
"$icons/": ["src/styles/assets/icons/*"]
}
}
}
Let me check the knowledge base for you again. One moment! :books:
That import pattern is fine; using a TypeScript path alias like $styles/* does not prevent Astro from optimizing the image, as long as the resolved file is inside src/. [Aliases; Image src rules]
From the docs:
- Local images in
src/must be imported (relative path or alias) and then passed directly assrcto<Image />. [Images in .astro; Image src rules] - By default,
<Image />outputs.webpunless you overrideformat. [format]
So this is valid:
---
import { Image } from 'astro:assets';
import heroAstride from '$styles/assets/hero/astride.png';
---
<Image
src={heroAstride}
alt="Illustration d'une personnage"
class="animate-hero-figure h-auto w-full select-none"
loading="eager"
width={720}
/>
If you’re still getting .png:
- The knowledge sources don’t describe alias-related bugs or other reasons why a local
pngwould staypngwhen used with<Image />, so I can’t say why with certainty. - You can force the format explicitly:
<Image
src={heroAstride}
alt="Illustration d'une personnage"
width={720}
format="webp"
/>