Communitygithub.com

YepAPI/技能

处理上传、调整/压缩图片、CDN 分发、模糊占位图以及 next/image 使用模式。

技能 是什么?

该技能专注于 Next.js 项目中图片的全面优化,从上传到最终展示提供完整的解决方案。它规定了使用 `next/image` 组件处理所有图片以实现自动优化、懒加载和响应式 `srcset`,并通过 `width` 和 `height` 或 `fill` 属性配合容器来防止布局偏移 (CLS)。技能涵盖了生成模糊占位图 (`blurDataURL`) 的最佳实践,包括在构建/上传时使用 `plaiceholder` 或对静态导入直接使用 `placeholder="blur"`。对于远程图片,需要在 `next.config.js` 中配置 `remotePatterns` 以白名单 CDN 域名。上传流程被描述为客户端 → 预签名 URL (例如 S3/R2/Supabase Storage) → 直接上传 → 将 URL 存入数据库。关键的优化步骤是使用 `sharp` 库在服务端进行图片调整:生成缩略图、中等尺寸和全尺寸版本,避免直接使用原始图片。最终通过 `next/image` 自动提供 WebP/AVIF 格式,并降级为 JPEG 以保证兼容性。图片通过 CDN 分发,并强调了文件大小验证(客户端和服务器端)以及 MIME 类型校验。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/image-optimization

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

图片优化

规则

  • 在 Next.js 中对所有图片使用 next/image — 自动优化、懒加载、响应式 srcset
  • 始终设置 width 和 height,或使用 fill 配合固定容器 — 防止布局偏移 (CLS)
  • 模糊占位图:在构建/上传时使用 plaiceholder 生成 blurDataURL,或对静态导入使用 placeholder="blur"
  • 远程图片:在 next.config.js 中配置 remotePatterns — 白名单 CDN 域名
  • 上传流程:客户端 → 预签名 URL (S3/R2/Supabase Storage) → 直接上传 → 将 URL 存入数据库
  • 上传时调整大小:使用 sharp 在服务端创建多种尺寸(缩略图、中等、全尺寸)— 不要提供原始图片
  • 格式:通过 next/image 自动格式提供 WebP/AVIF — 降级为 JPEG 以保证兼容性
  • CDN 分发:将原始图片存储在对象存储中 (S3, R2),通过 CDN (CloudFront, Cloudflare) 提供服务,并设置缓存头
  • 最大文件大小验证:在客户端(上传前)和服务器端(处理前)进行检查 — 典型限制为 5-10MB

模式

// 使用模糊占位图的 next/image
import Image from "next/image";

<Image
  src="/hero.jpg"
  alt="英雄图片"
  width={1200}
  height={630}
  placeholder="blur"
  blurDataURL="data:image/jpeg;base64,/9j/4AAQ..."
  priority // 首屏
/>

// 动态容器的填充模式
<div className="relative aspect-video">
  <Image src={url} alt={alt} fill className="object-cover" sizes="(max-width: 768px) 100vw, 50vw" />
</div>
// 使用 sharp 在服务端调整大小
import sharp from "sharp";

async function processUpload(buffer: Buffer) {
  const thumbnail = await sharp(buffer).resize(200, 200, { fit: "cover" }).webp({ quality: 80 }).toBuffer();
  const medium = await sharp(buffer).resize(800).webp({ quality: 85 }).toBuffer();
  const full = await sharp(buffer).resize(1920).webp({ quality: 90 }).toBuffer();
  return { thumbnail, medium, full };
}

避免

  • 在 Next.js 中使用 <img> 标签 — 会失去自动优化、懒加载和格式转换
  • 响应式图片缺少 sizes 属性 — 浏览器无法选择合适的 srcset 尺寸
  • 提供未调整大小的原始图片 — 将 5MB 的照片显示为 400px 是浪费
  • 跳过文件类型验证 — 在服务端检查 MIME 类型,而不仅仅是文件扩展名

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Tenant isolation (row-level vs schema), subdomain routing, per-tenant config, and data partitioning.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能