Communitygithub.com

sakilanwar12/nextjs-typescript-skills

用於最佳化 Next.js 效能 — 圖片/字型最佳化、Bundle 分析、動態載入、ISR/PPR 快取以及 Core Web Vitals 修復。

nextjs-typescript-skills 是什麼?

此技能專注於 Next.js 應用的效能調校,涵蓋圖片與字型最佳化、Bundle 分析、動態載入、ISR/PPR 快取策略以及 Core Web Vitals 指標的改善。它明確提供了具體的規則與程式碼範例,例如強制使用 `next/image` 搭配明確的 `sizes` 屬性、使用 `next/font` 取代外部字型連結,以及利用 `dynamic(() => import(...), { ssr: false })` 對重量的客戶端元件進行程式碼分割。此外,也包含了使用 `@next/bundle-analyzer` 進行 Bundle 分析的設定,並提供了 ISR 快取與串流 Suspense 的模式,以減少 TTFB。最後以一個清單形式,協助開發者確保 LCP、CLS、INP 等核心指標已得到妥善處理。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/sakilanwar12/nextjs-typescript-skills/tree/HEAD/skills/nextjs-performance

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Next.js 效能

規則

  1. 圖片:一律使用 next/image,並指定明確的 sizes;priority 僅用於 LCP 英雄區塊。字型:使用 next/font(不採用會阻塞渲染的 Google <link>)。
  2. 先測量再最佳化:使用 @next/bundle-analyzer、next build 輸出、Lighthouse。先修復最大的區塊。
  3. 對於重量級的客戶端專用小工具(圖表、編輯器),使用 dynamic(() => import(...), { ssr: false })。盡可能將初始 JS 保持在 200KB gz 以下。
  4. 積極快取:對於半動態頁面使用靜態 + revalidate (ISR);使用 loading.tsx/Suspense 串流傳輸慢速部分,避免阻塞 TTFB。
  5. 避免瀑布效應:在 RSC 中平行處理,記憶化重複的 fetch(每個請求自動去重),對昂貴的計算使用 unstable_cache。

模式

// next/image — 指定尺寸,預設為懶加載
import Image from 'next/image';
<Image src="/hero.jpg" alt="儀表板" width={1200} height={630} priority sizes="100vw" />
<Image src="/avatar.png" alt="使用者" width={64} height={64} sizes="64px" />

// next/font — 自託管,零 CLS
import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'], display: 'swap' });
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return <html lang="en" className={inter.className}><body>{children}</body></html>;
}

// 重量級客戶端小工具 — 程式碼分割
import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('@/components/chart'), { ssr: false, loading: () => <p>正在載入圖表…</p> });

// ISR
export const revalidate = 60;
// next.config.js — 分析器
const withBundleAnalyzer = require('@next/bundle-analyzer')({ enabled: process.env.ANALYZE === 'true' });
module.exports = withBundleAnalyzer({});
// ANALYZE=true next build

檢查清單

  • 英雄區塊圖片是否已設定 priority + sizes,其他圖片是否懶加載?
  • 是否使用了 next/font,且沒有外部字型連結?
  • 重量級元件是否已動態匯入?
  • 是否已分析 Bundle,且最大的匯入是有正當理由的?
  • 對於可快取的頁面是否已設定 ISR/revalidate?
  • 是否已在 Lighthouse 中檢查 LCP/CLS/INP?

Individual skills in this repo

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

相關技能