Communitygithub.com

YepAPI/skills

上傳處理、調整大小/壓縮、CDN 傳遞、模糊佔位圖片以及 next/image 模式。

skills 是什麼?

此技能專注於 Next.js 應用程式中的圖片優化,提供一套完整的上傳、處理與傳遞流程。它涵蓋了從客戶端通過預簽名 URL 上傳圖片、使用 sharp 進行伺服器端調整大小與轉換格式、生成模糊佔位圖以提升用戶體驗、以及設定 CDN 快取與 next/image 組件的正確配置。支援平台包含 Next.js、S3/R2/Supabase Storage 物件儲存、CloudFront/Cloudflare CDN,並使用 sharp 進行圖片處理。此技能明確規範了應使用 next/image 組件、設定寬高或 fill 屬性以避免 CLS、在伺服器端驗證檔案類型與大小、以及透過 blurDataURL 提供模糊佔位效果。它同時提供清晰的 TypeScript/JSX 代碼範例,說明如何實作響應式圖片與伺服器端處理。

相容平台~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 // 首屏內容
/>

// 動態容器的 Fill 模式
<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 prop — 瀏覽器無法選擇正確的 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.

相關技能