Next.js Performance
Rules
- Images: always
next/image with explicit sizes, priority only for LCP hero. Fonts: next/font (no render-blocking Google <link>).
- Measure first:
@next/bundle-analyzer, next build output, Lighthouse. Fix the biggest chunk first.
dynamic(() => import(...), { ssr: false }) for heavy client-only widgets (charts, editors). Keep initial JS < 200KB gz where possible.
- Cache aggressively: static +
revalidate (ISR) for semi-dynamic pages; loading.tsx/Suspense to stream slow parts instead of blocking TTFB.
- Avoid waterfalls: parallelize in RSC, memoize repeated
fetch (auto-deduped per request), use unstable_cache for expensive computes.
Patterns
import Image from 'next/image';
<Image src="/hero.jpg" alt="Dashboard" width={1200} height={630} priority sizes="100vw" />
<Image src="/avatar.png" alt="User" width={64} height={64} sizes="64px" />
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>Loading chart…</p> });
export const revalidate = 60;
const withBundleAnalyzer = require('@next/bundle-analyzer')({ enabled: process.env.ANALYZE === 'true' });
module.exports = withBundleAnalyzer({});
Checklist