이미지 및 글꼴 최적화
Next.js에 내장된 이미지 및 글꼴 구성 요소를 활용하여 자동으로 최적화하고 로딩 성능을 향상합니다.
이미지 및 글꼴 최적화은(는) CoddyKit의 무료 Next.js 15 Fullstack (App Router + Server Actions) 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Next.js 15 Fullstack (App Router + Server Actions) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Next.js 15 Fullstack (App Router + Server Actions) 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Optimize Assets?
Web performance is crucial for user experience and SEO. Large images and unoptimized fonts can significantly slow down your website.
Next.js provides built-in components to automatically optimize these assets, making your applications faster and more efficient.
Image Loading Challenges
Traditionally, developers face challenges with images:
- Large File Sizes: Slow downloads, high bandwidth usage.
- Incorrect Dimensions: Serving huge images to small screens.
- Layout Shifts (CLS): Images loading late, pushing content around.
- Accessibility: Missing
altattributes.
Meet `next/image`
Next.js's Image component (from next/image) is a powerful tool designed to solve these problems automatically. It handles:
- Automatic Optimization: Converts images to modern formats like WebP.
- Responsive Sizing: Serves different image sizes based on device.
- Lazy Loading: Loads images only when they enter the viewport.
- Preventing CLS: Ensures images occupy space before loading.
Basic `next/image` Usage
To use Image, simply import it and replace your standard <img> tags. You must provide width, height, and alt props.
Try running this example (assuming /my-image.jpg is in your public folder):
import Image from 'next/image';
export default function HomePage() {
return (
<div>
<h1>Optimized Image</h1>
<Image
src="/my-image.jpg"
alt="A beautiful landscape"
width={500}
height={300}
/>
<p>This image is now optimized!</p>
</div>
);
}Key Image Props
Let's look at essential props for the Image component:
src: Path to your image (local or external).alt: Crucial for accessibility and SEO.width,height: Specify intrinsic dimensions to prevent layout shifts.priority: (Optional) Load image immediately if it's above the fold.
Image Layout with `fill`
For images that should fill their parent container, use the fill prop. This is great for hero images or backgrounds.
The parent element needs position: 'relative' to act as the container for the filled image.
import Image from 'next/image';
export default function Banner() {
return (
<div style={{
position: 'relative',
width: '100%',
height: '200px',
backgroundColor: '#eee'
}}>
<Image
src="/banner.jpg"
alt="Website banner"
fill
style={{ objectFit: 'cover' }}
sizes="(max-width: 768px) 100vw, 50vw"
/>
<h2 style={{ position: 'absolute', color: 'white', zIndex: 10 }}>
Awesome Banner
</h2>
</div>
);
}The Need for Font Optimization
Custom fonts add personality to your site, but they can be large files, leading to performance issues like:
- FOIT (Flash of Invisible Text): Text is invisible until the font loads.
- FOUT (Flash of Unstyled Text): Text displays in a default font, then switches.
- Large Bundles: Font files increase overall page weight.
Next.js's next/font module helps tackle these challenges.
Optimizing Google Fonts
next/font/google automatically optimizes Google Fonts, downloading them at build time and self-hosting them with strong caching. This eliminates external network requests and ensures consistent loading.
Apply the font's class name to your HTML or a specific element:
import { Inter } from 'next/font/google';
const inter = Inter({
subsets: ['latin'],
display: 'swap', // Prevents FOIT
});
export default function AppLayout({ children }) {
return (
<html lang="en" className={inter.className}>
<body>
<h1>My App Header</h1>
{children}
</body>
</html>
);
}Using Local Fonts
For self-hosted fonts (fonts you provide), use next/font/local. It works similarly to next/font/google, optimizing the font loading process for your local files.
Place your font files (e.g., .woff2) in the public directory or a dedicated fonts folder.
import localFont from 'next/font/local';
const myCustomFont = localFont({
src: './fonts/MyCustomFont.woff2',
display: 'swap',
});
export default function AboutPage() {
return (
<div className={myCustomFont.className}>
<h2>About Us</h2>
<p>This page uses a custom font.</p>
</div>
);
}Optimize Your Assets
Which of the following are benefits of using Next.js's Image and Font components for asset optimization?
Recap: Optimized Assets
You've learned how Next.js empowers you to optimize critical assets like images and fonts:
- The
next/imagecomponent offers automatic optimization, lazy loading, and CLS prevention. - The
next/fontmodule ensures efficient loading of both Google and local fonts, improving performance and visual stability.
By using these components, you significantly enhance your application's speed and user experience!
자주 묻는 질문
“이미지 및 글꼴 최적화” 강의는 무료인가요?
네 — “이미지 및 글꼴 최적화” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Next.js 15 Fullstack (App Router + Server Actions) 강의 전체를 잠금 해제할 수 있습니다. Next.js 15 Fullstack (App Router + Server Actions) 강의에는 총 4개의 강의가 포함되어 있습니다.
“이미지 및 글꼴 최적화”에서 뭘 배우나요?
Next.js에 내장된 이미지 및 글꼴 구성 요소를 활용하여 자동으로 최적화하고 로딩 성능을 향상합니다. 브라우저에서 직접 실행하는 실습 코드로 Next.js 15 Fullstack (App Router + Server Actions)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Next.js 15 Fullstack (App Router + Server Actions)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Next.js 15 Fullstack (App Router + Server Actions)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“이미지 및 글꼴 최적화” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Next.js 15 Fullstack (App Router + Server Actions) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Next.js 15 Fullstack (App Router + Server Actions) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 이미지 및 글꼴 최적화
- 번들 크기 분석
- 고급 캐싱 전략
- Suspense와 로딩 상태를 활용한 UI 스트리밍