image-optimization
图片优化:astro:assets 的 Image 与 getImage(仅服务端)、resolveCover 封面解析、远程图片授权、构建期转存防盗链、首屏 eager 与 priority、alt 与宽高、og…
by bkywksj★ 55 GitHub stars
Install image-optimization in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
图片优化(next-ai-astro)
何时使用
- 数据里的封面是外链(B 站、GitHub、CDN)要显示在站内
- 图片加载慢、LCP 差、布局跳动(CLS)
- 外链图片在站内不显示(防盗链 / Referer)
- 要加本地图片、做响应式图片(srcset)、转 WebP / AVIF
- 设置分享图(og:image)
- 写 alt 文本
概述
内容站的图片(封面、头图)是首屏最重的资源,也是 SEO 与 AI 抓取的重要信号。本框架的原则:
- 能在构建期处理的都在构建期处理:裁切、转 WebP、生成多尺寸 srcset、把外链图转存为站内文件。
- 每张
<img>都有宽高(防布局偏移)和alt(装饰图alt="")。 - 首屏一行图不懒加载(eager),其中只有 1 张高优先级(priority),其余懒加载。
本框架现状
src/lib/images.ts
export async function resolveCover(src: string, width = 640, height = 360): Promise<ResolvedImage> {
if (!isOptimizableRemote(src)) {
return { src, width, height }; // 本地 public/ 路径、未授权外链:原样返回
}
const image = await getImage({
src, width, height, fit: 'cover', format: 'webp',
widths: [Math.round(width * 0.625), width, Math.round(width * 1.5)],
});
return { src: image.src, srcSet: image.srcSet.attribute || undefined, width, height };
}
isOptimizableRemote(src):http(s)外链且 hostname 在site.imageDomains里才优化。- 列表卡片:
toCardData()调resolveCover(cover)(640×360),结果进WorkCardData.cover,由WorkCard渲染。 - 详情头图:
resolveCover(cover, 1280, 720),loading="eager"+fetchpriority="high"(本页唯一的高优先级图)。 astro.config.ts:image: { domains: site.imageDomains }。- 示例封面是
public/examples/*.svg(本地路径,不走优化);分享图默认public/og-default.png(1200×630)。
WorkCard 的图片属性
<img
src={work.cover.src}
srcSet={work.cover.srcSet}
sizes="(min-width: 1280px) 400px, (min-width: 640px) 50vw, 100vw"
width={work.cover.width}
height={work.cover.height}
alt="" // 卡片标题就在旁边,封面是装饰:读屏不重复朗读
loading={eager ? 'eager' : 'lazy'} // eager:不懒加载
decoding="async"
fetchPriority={priority ? 'high' : 'auto'} // priority:只给整页那 1 张 LCP 候选
/>
eager 与 priority 是两个独立 prop(口径与 ui-components 一致):
| 位置 | eager(不懒加载) |
priority(fetchpriority=high) |
|---|---|---|
卡片网格在首屏的页面(列表页 /works/、分类页、首页精选) |
前 3 张(桌面端第一行) | 只有第 1 张 |
| 详情页 | 头图(页面里直接写的 <img>,loading="eager") |
头图 fetchpriority="high";相关推荐卡片都不设 |
各页面当前的具体取值以页面源码为准;新页面照这个口径写:eager={index < 3} priority={index === 0}。
标准做法
1. 外链封面:加白名单,构建期转存
// src/site.config.ts
imageDomains: ['i0.hdslb.com', 'i1.hdslb.com', 'i2.hdslb.com'],
加入后,getImage() 会在构建期下载原图、裁切、转 WebP、生成多个宽度,输出到 dist/_astro/。
页面上引用的是站内地址,于是:
- 防盗链问题消失:防盗链一般校验浏览器请求的
Referer;构建期由 Node 在服务端下载,访客浏览器根本不再请求原站。 - 不依赖第三方 CDN 可用性,图片与站点同域同缓存策略。
- 体积与尺寸可控(原图可能是几 MB 的大图)。
官方说明(images 指南):未授权的远程图片不会被优化,<Image /> 仍可防止布局偏移;inferSize 自 5.17.3 起也只对已授权域名生效。
🔴 白名单以实际数据为准:同步完数据后检查 src/data/*.json 里封面的真实域名再填,不要凭印象。
另外数据源映射时把 http:// 统一改 https://(见 data-sync)。
2. getImage() 只能在服务端
Astro 6 起在客户端调用 getImage() 会直接抛错。正确流程:
页面 frontmatter / .astro 组件 ──getImage()──▶ { src, srcSet, width, height } ──props──▶ React island / WorkCard
island 里只拿结果渲染 <img>,不 import astro:assets。
3. 选 <Image /> 还是 getImage()
| 场景 | 用 |
|---|---|
.astro 组件里渲染单张图 |
<Image />(自动生成 width/height/srcset) |
| 结果要传给 React 组件、或要同时拿 URL 做 og:image / JSON-LD | getImage()(本框架封装成 resolveCover) |
| 纯装饰 SVG、极小图标 | 直接 <img> 或内联 SVG |
<Image /> 远程图片需要宽高:要么传 width + height,要么 inferSize(仅授权域名):
---
import { Image } from 'astro:assets';
---
<Image src={remoteUrl} width={640} height={360} alt="…" />
<Image src={remoteUrl} inferSize alt="…" />
本地图片(放 src/assets/,import 引入)可用响应式布局:
---
import { Image } from 'astro:assets';
import hero from '@/assets/hero.png';
---
<Image src={hero} alt="…" layout="constrained" width={800} height={600} />
layout="constrained" 会自动生成 srcset 与 sizes;Astro 6 起响应式图片样式通过 data-* 属性实现,不再写内联 style。
4. 本地图片放哪
| 位置 | 是否优化 | 用于 |
|---|---|---|
src/assets/ + import |
是(哈希文件名、格式转换、尺寸) | 页面里的内容图、头图 |
public/ |
否(原样复制,路径固定) | favicon、og 默认图、robots 等需要固定 URL 的文件 |
5. 首屏与懒加载
loading="eager"(不懒加载)给首屏那一行:卡片网格前 3 张、详情页头图。fetchpriority="high"整页只给 1 张(LCP 候选):卡片网格第 1 张,或详情页头图。多张同时 high 等于没有优先级,还会互相抢带宽。- 其余一律
loading="lazy"+decoding="async"。 - 用
<Image />渲染首屏图时可直接传priority(Astro 5.10+,自动把loading/decoding/fetchpriority设为首屏图的最优值, 见astro:assets参考),同样全页只用一次。 sizes要贴合真实布局:列表三栏时单卡约 400px,写对sizes浏览器才会选小图。
6. alt 规则
| 图片类型 | alt |
|---|---|
| 卡片封面(旁边就是标题链接) | alt=""(装饰,避免读屏重复) |
| 详情页头图 | alt={\${title} 封面`}` 或更具体的描述 |
| 文章内容图 | 描述图中信息(图表写结论,截图写界面名称与要点) |
| 纯装饰(光斑、分隔) | alt="" 或用 CSS 背景 |
pnpm check:seo 会检查每张 <img> 是否有 alt 属性、是否声明宽高。
7. og 分享图
- 默认
site.ogImage(public/og-default.png,1200×630)。 - 详情页把封面传给
BaseLayout的image(works/[id].astro已这样做),SEOHead会转成绝对地址。 - 需要为每条内容生成定制分享图时:构建期生成 PNG 放到可访问路径(例如用 Satori 类工具在端点里渲染), 这属于扩展,先评估构建耗时。
扩展做法
remotePatterns(按规则放行)
域名多、有子域名规律时用模式匹配(官方 images 指南):
image: {
remotePatterns: [{ protocol: 'https', hostname: '**.hdslb.com' }],
},
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the image-optimization skill do?
图片优化:astro:assets 的 Image 与 getImage(仅服务端)、resolveCover 封面解析、远程图片授权、构建期转存防盗链、首屏 eager 与 priority、alt 与宽高、og 分享图。触发词:图片、封面、astro:assets、getImage、Image组件、WebP、AVIF、远程图片、防盗链、懒加载、og图、分享图
How do I install the image-optimization skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the image-optimization skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
AnthropicToolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
access
AnthropicManage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
configure
AnthropicSet up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
access
AnthropicManage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…