惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
T
The Exploit Database - CXSecurity.com
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
博客园 - Franky
美团技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
腾讯CDC
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
T
Tor Project blog
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
Cyberwarzone
Cyberwarzone
V
V2EX
T
Tenable Blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
K
Kaspersky official blog
Latest news
Latest news
S
Schneier on Security
Martin Fowler
Martin Fowler
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
Microsoft Security Blog
Microsoft Security Blog
S
Securelist
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
L
LangChain Blog
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
T
Threatpost
有赞技术团队
有赞技术团队

The Astro Blog

Astro 6.4 Astro 6.3 Starlight 0.39 Astro 6.2 What's new in Astro - April 2026 What's new in Astro - March 2026 Astro 6.1 CloudCannon Joins Astro as an Official CMS Partner Astro 6.0 What's new in Astro - February 2026 What's new in Astro - January 2026 Astro 5.17 Supporting the future of Astro The Astro Technology Company joins Cloudflare Astro 6 Beta What's new in Astro - December 2025 What's new in Astro - November 2025 Astro 5.16 Stainless Sponsors Astro, Launches Astro-Powered Docs Platform What's new in Astro - October 2025 Astro 5.15 Spirit of Astro: meet the winning designs What's new in Astro - September 2025 Astro 5.14 Cloudflare Donates $150,000 to Support Astro's Open Source Mission Webflow Donates $150,000 to Support Astro's Open Source Mission Mux: Our Official Video Partner Unleashing creativity: How CodeTV built a video streaming platform with Astro and Mux | Astro What's new in Astro - August 2025 Astro 5.13 What's new in Astro - July 2025 Astro 5.12 Starlight 0.35 Astro 5.11 What's new in Astro - June 2025 Live Content Collections: A Deep Dive Introducing… Astro Mart Astro Solstice Festival Astro 5.10 Astro 5.9 What's new in Astro - May 2025 Astro 5.8 What's new in Astro - April 2025 2025 Technical Steering Committee Starlight April Update Astro 5.7 Astro Agency Partner Program Astro 5.6 What's new in Astro - March 2025 Astro 5.5 What's new in Astro - February 2025 Astro 5.4 Starlight 0.32 Astro 5.3 What's new in Astro - January 2025 Astro 5.2 2024 year in review What's new in Astro - December 2024 Astro 5.1 Astro 5.0 Google IDX: Our Official Online Editor Partner What's new in Astro - November 2024 What's new in Astro - October 2024 Astro x Cloudinary SDK What's new in Astro - September 2024 Community Loaders for Astro Content Layer Astro x Hygraph: Content Loader Astro x Cloudinary: Content Loader Astro x Storyblok: Content Loader Content Layer: A Deep Dive Starlight 0.28 Astro 5.0 Beta Release The $100,000 Astro Ecosystem Fund Fall Update Goodbye Studio, Hello DB What's new in Astro - August 2024 Astro 4.15 Astro 4.14 Astro 4.13 What's new in Astro - July 2024 Astro 4.12: Server Islands Netlify: Our Official Deployment Partner What's new in Astro - June 2024 Astro 4.11 Astro Together 2024 Server Islands The Astro Content Layer Zero-JavaScript View Transitions Astro 4.10 Starlight turns one year old! What's new in Astro - May 2024 Astro 4.9 Astro 4.8 What's new in Astro - April 2024 Astro 4.7 Astro 4.6 What's new in Astro - March 2024 Migrating 500+ tests from Mocha to Node.js Astro DB: A Deep Dive The Astro Developer Portal Astro DB
Better Images in Astro
Erika, Fred Schott · 2023-06-07 · via The Astro Blog

Last year, we published the first Astro image integration (@astrojs/image) alongside Astro 1.0. The goal was simple: Make image optimization effortless in Astro.

Now, 1 year and 2.5 million downloads later, we’re ready to share our new vision for working with images in Astro: completely redesigned with new features, faster performance, and a better end-user experience with:

  • Automatic layout shift (CLS) prevention.
  • First-class Markdown, Markdoc & MDX support.
  • Integration with third-party services.

This new update will be available for all users starting in Astro 3.0, but you can opt-in today by setting the experimental.assets flag in your configuration file. You can also enable by passing the --experimental-assets flag to the Astro CLI.

import { defineConfig } from "astro/config"

export default defineConfig({

experimental: { assets: true },

})

In this post, we’ll go over some of the new features and improvements that will make working with images in Astro even easier. If you prefer to jump right into the docs, read our Assets guide for all of the details.

How it works

From the start, we knew that we wanted an image API that felt as minimal as possible. We started with the original <Image /> Component API from the @astrojs/image integration, and then built on it with a few new tweaks and changes.

---

// Import the <Image /> component

import { Image } from "astro:assets"

// Import a reference to the image itself

import myImage from "../assets/penguin.png"

---

<Image src={myImage} alt="A very cool penguin!" />

<!-- Result: -->

<img

src="/_astro/penguin.XXXXXX.webp"

width="300"

height="300"

loading="lazy"

decoding="async"

alt="A very cool penguin!"

/>

But don’t let the minimal API fool you:

  • Astro optimizes your image using the modern WebP file format (by default).
  • For accessibility, the required alt prop helps improve your site for screen-readers and other tooling.
  • For even faster page-load performance, the loading and decoding attributes are added as defaults.
  • For the user experience, The width and height attributes are always included to prevent layout shift (more on this later).

Flexibility was another goal. We added the getImage() function to optimize an image in your project, on-demand, without using the <Image /> component. Use this function to build your own custom Image component for any use-case you have in mind.

---

// Example: Instead of creating an `img` tag,

// apply an optimized image to a div using CSS.

import { getImage } from "astro:assets"

import myImage from "../assets/penguin.png"

const optimizedBackgroundImage = await getImage({ src: myImage })

---

<div style={`background-image: url('${optimizedBackgroundImage.src}')`}></div>

<!-- Result: -->

<div style="background-image: url('/_astro/penguin.XXXXXX.webp')"></div>

Automatic layout shift prevention

Cumulative Layout Shift (CLS) is an essential metric to optimize for website performance. It is tracked as one of Google’s 3 Core Web Vitals and is weighted heavily in the Lighthouse performance score.

Astro’s new <Image /> component automatically protects your site from layout shift. It works by inferring an explicit height and width for every image and including them in the generated <img> tag. With explicit dimensions, the image can’t shift the page layout as it loads onto the page.

The output HTML is always a simple <img> element, so you can also apply additional CSS styling for more advanced responsive layouts like fill and cover without risking layout shift.

<Image

src={myImage}

alt="A very cool penguin!"

style="height: 100%; width: 100%; object-fit: cover;"

/>

Automatic Markdown & MDX support

One of the biggest improvements made over the original @astrojs/image integration is complete support for Markdown, Markdoc, and MDX. You can now reference images using a relative image path in your Markdown content or frontmatter, and Astro will automatically optimize them for you.

![A starry night sky](../../assets/stars.png)

<!-- Result: -->

<img

src="/_astro/stars.XXXXXX.webp"

width="300"

height="300"

loading="lazy"

decoding="async"

alt="A starry night sky"

/>

That’s it! Your stars.png image will be optimized automatically in the final HTML. You can also use the <Image /> component directly in MDX if you prefer for finer control over the image attributes, but in most cases the default settings will be enough to produce an optimized file size with a great image quality.

Integrating with third-party image services

Some users will inevitably want to use Astro with an external image service, like Cloudinary or Vercel. With extensibility in mind, we built the Astro Image Services API to connect Astro with the third-party image service of your choice.

This API contains a small collection of hooks that can control image handling inside of Astro. Most Astro users will never need to touch this API directly, but integration authors can use these to extend Astro’s default behavior with their own image handling logic.

A simple image service exports a getURL() function to create the src URL for each image that points to the image service domain or custom URL path:

// Example: A basic image service that generates custom URLs

export const service = {

getURL({ src, width, height }) {

return `https://mywebsitethatgeneratesimages.com/${src}?width=${width}&height=${height}`

},

}

More complex image services can use hooks like transform() to directly generate and return an image to be served, at build-time. For a walkthrough of the full API, check out our Image Services documentation.

Vercel users can try this today with the official Vercel adapter for Astro. Anyone using this adapter will see their images use the Vercel-specific URL endpoint that allows Vercel to handle the image optimization for you.

We’re looking forward to seeing our community use this feature to integrate Astro with their favorite services, and add to the growing list of Astro integrations!

What next?

We’ll continue to gather your feedback as we move closer to an official release in Astro 3.0 later this year. If you find any issues, do not hesitate to let us know, either by creating an issue, or on our Discord.

There’s plenty that we didn’t get to cover here, like caching between builds, controlling the file format, and MDX support. For more information on how to use this feature, please visit our assets documentation.