typescriptbeginner
Font Optimization with next/font
Use next/font for automatic self-hosted font optimization with zero layout shift.
typescriptPress ⌘/Ctrl + Shift + C to copy
// app/layout.tsx
import { Inter, JetBrains_Mono } from 'next/font/google';
import localFont from 'next/font/local';
const inter = Inter({
subsets: ['latin'],
display: 'swap',
variable: '--font-inter',
});
const jetbrainsMono = JetBrains_Mono({
subsets: ['latin'],
display: 'swap',
variable: '--font-mono',
});
const calSans = localFont({
src: '../fonts/CalSans-SemiBold.woff2',
variable: '--font-cal',
display: 'swap',
});
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html
lang="en"
className={`${inter.variable} ${jetbrainsMono.variable} ${calSans.variable}`}
>
<body className="font-sans">{children}</body>
</html>
);
}
// tailwind.config.ts — reference CSS variables
// fontFamily: {
// sans: ['var(--font-inter)'],
// mono: ['var(--font-mono)'],
// cal: ['var(--font-cal)'],
// }Use Cases
- custom typography
- performance optimization
- preventing CLS
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptbeginner
Next.js Image Optimization Patterns
Advanced next/image usage with responsive sizes, blur placeholders, and custom loaders.
Best for: Optimizing Core Web Vitals with proper image loading
#nextjs#image
typescriptintermediate
Next Image Responsive Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal image delivery.
Best for: responsive images
#nextjs#image
typescriptbeginner
Next.js Image Optimization Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal Core Web Vitals.
Best for: Hero images
#images#optimization
typescriptintermediate
Next.js Streaming with Suspense
Stream server components with Suspense boundaries for progressive page loading and better TTFB.
Best for: Progressive loading for data-heavy dashboards
#nextjs#streaming