Images, Fonts & Static Assets
⏱ 12 min read read
next/image --- Automatic Optimisation:
Always use next/image instead of <img>. It automatically resizes,
converts to WebP/AVIF, lazy-loads, and prevents layout shift.
import Image from 'next/image';
// Local image (from public/ folder)
<Image src="/hero.png" alt="Hero" width={1200} height={630}
priority />
// Remote image (must configure in next.config.js)
<Image
src="https://example.com/photo.jpg"
alt="Photo"
width={400}
height={300}
className="rounded-lg"
/>
// Fill parent container
<div style={{ position: 'relative', width: '100%', height: 400
}}>
<Image src="/bg.jpg" alt="Background" fill style={{ objectFit:
'cover' }} />
</div>
Allowing Remote Images in next.config.js:
// next.config.js
module.exports = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'example.com' },
{ protocol: 'https', hostname: '*.githubusercontent.com' },
],
},
};
next/font --- Zero Layout Shift Fonts:
// app/layout.tsx
import { Inter, Fira_Code } from 'next/font/google';
const inter = Inter({ subsets: ['latin'], variable:
'--font-sans' });
const firaCode = Fira_Code({ subsets: ['latin'], variable:
'--font-mono' });
export default function RootLayout({ children }: { children:
React.ReactNode }) {
return (
<html lang="en" className={`${inter.variable}
${firaCode.variable}`}>
<body>{children}</body>
</html>
);
}
next/font downloads and self-hosts Google Fonts at build time --- no
external request.
Static files in public/ are served at / --- public/logo.png →
/logo.png.
next/image requires width + height OR fill prop --- prevents CLS
(layout shift).
Use priority on above-the-fold images to avoid LCP penalty.
Always use next/image instead of <img>. It automatically resizes,
converts to WebP/AVIF, lazy-loads, and prevents layout shift.
import Image from 'next/image';
// Local image (from public/ folder)
<Image src="/hero.png" alt="Hero" width={1200} height={630}
priority />
// Remote image (must configure in next.config.js)
<Image
src="https://example.com/photo.jpg"
alt="Photo"
width={400}
height={300}
className="rounded-lg"
/>
// Fill parent container
<div style={{ position: 'relative', width: '100%', height: 400
}}>
<Image src="/bg.jpg" alt="Background" fill style={{ objectFit:
'cover' }} />
</div>
Allowing Remote Images in next.config.js:
// next.config.js
module.exports = {
images: {
remotePatterns: [
{ protocol: 'https', hostname: 'example.com' },
{ protocol: 'https', hostname: '*.githubusercontent.com' },
],
},
};
next/font --- Zero Layout Shift Fonts:
// app/layout.tsx
import { Inter, Fira_Code } from 'next/font/google';
const inter = Inter({ subsets: ['latin'], variable:
'--font-sans' });
const firaCode = Fira_Code({ subsets: ['latin'], variable:
'--font-mono' });
export default function RootLayout({ children }: { children:
React.ReactNode }) {
return (
<html lang="en" className={`${inter.variable}
${firaCode.variable}`}>
<body>{children}</body>
</html>
);
}
next/font downloads and self-hosts Google Fonts at build time --- no
external request.
Static files in public/ are served at / --- public/logo.png →
/logo.png.
next/image requires width + height OR fill prop --- prevents CLS
(layout shift).
Use priority on above-the-fold images to avoid LCP penalty.
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress