Display images
You show Hako CMS image fields at a size and format that fit the screen. When you add parameters to an image URL, Hako CMS returns the image with a different width or format. So you can serve images that fit the screen width without transforming images on the Astro side.
Prerequisites
Section titled “Prerequisites”- You can already read entries. Read them with one of the approaches in Use Hako CMS with Astro
- The content type has an image field. In the example below, it is “eyecatch”
An image field is returned in the following form. width, height, and alt may be missing.
"eyecatch": { "url": "https://<media host>/media/k2m9x0a1b2c3/hero.jpg", "width": 1600, "height": 900, "alt": "トップの写真"}Do not build url yourself. Use the returned value as is.
To show images that fit the screen, do the following.
-
Write a component that shows images in
src/components/HakoImage.astro. It outputs WebP images at 1x and 2x the display width and lets the browser choose.src/components/HakoImage.astro ---type Media = { url: string; width?: number; height?: number; alt?: string };interface Props {image: Media;/** Display width (px) */width: number;/** How the display width relates to the screen width (img sizes) */sizes?: string;}const { image, width, sizes = `${width}px` } = Astro.props;// Transforming a GIF stops its animation, so output the original imageconst gif = new URL(image.url).pathname.toLowerCase().endsWith(".gif");// fit=max: do not make it larger than the original imageconst src = (w: number) =>gif ? image.url : `${image.url}?w=${w}&fit=max&fm=webp`;const srcset = gif? undefined: `${src(width)} ${width}w, ${src(width * 2)} ${width * 2}w`;// If the aspect ratio is known, set the height to avoid shifts on loadconst height =image.width && image.height? Math.round((width * image.height) / image.width): undefined;---<imgsrc={src(width)}srcset={srcset}sizes={gif ? undefined : sizes}width={width}height={height}alt={image.alt ?? ""}loading="lazy"decoding="async"/> -
Use it in a page.
post.eyecatchis the image field of the entry you read.---import HakoImage from "../../components/HakoImage.astro";// post is the entry you read---{post.eyecatch && (<HakoImageimage={post.eyecatch}width={800}sizes="(max-width: 800px) 100vw, 800px"/>)}An
imglike the following is output.<imgsrc="https://<media host>/media/k2m9x0a1b2c3/hero.jpg?w=800&fit=max&fm=webp"srcset="…?w=800&fit=max&fm=webp 800w, …?w=1600&fit=max&fm=webp 1600w"sizes="(max-width: 800px) 100vw, 800px"width="800" height="450" alt="トップの写真" loading="lazy" decoding="async"/>The
…insrcsetstands for the same URL assrc, omitted.If you add
fm=webpto a GIF, only the first frame is transformed and the animation is lost. So this component does not add transformation parameters to GIFs and outputs the original image.
Why not to use Astro’s image transformation
Section titled “Why not to use Astro’s image transformation”Astro’s <Image> can also transform external images. In that case, you add the media host to image.remotePatterns in astro.config.mjs, and Astro also transforms the image.
Hako CMS images can already be transformed through the URL. If Astro also transforms them, the same image is transformed twice, which increases either the build time or the cost of transforming on the server. When you use the Cloudflare adapter, you can set imageService: "passthrough" to turn off transformation on the Astro side (step 2 of Use with Cloudflare Workers).
Images in rich text
Section titled “Images in rich text”When you read rich text with richText=html, images in the body come in as img tags with the URL of the original size. Use CSS to fit their width so they do not overflow the screen.
article img { max-width: 100%; height: auto;}Verify
Section titled “Verify”Open the page and check the image requests in the browser’s developer tools. If the URL has w= and fm=webp and the response Content-Type is image/webp, transformed images are being served.
Related
Section titled “Related”- Transform images (size, format): the available parameters and which source images can be transformed
- Upload media
- Use Hako CMS with Astro