การปรับแต่งรูปภาพและแบบอักษร
ใช้คอมโพเนนต์ Image และ Font ในตัวของ Next.js เพื่อปรับแต่งโดยอัตโนมัติและเพิ่มประสิทธิภาพการโหลด
การปรับแต่งรูปภาพและแบบอักษร เป็นบทเรียน Next.js 15 Fullstack (App Router + Server Actions) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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!
เรียนรู้ TypeScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “การปรับแต่งรูปภาพและแบบอักษร” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การปรับแต่งรูปภาพและแบบอักษร” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack (App Router + Server Actions) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack (App Router + Server Actions) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การปรับแต่งรูปภาพและแบบอักษร”
ใช้คอมโพเนนต์ Image และ Font ในตัวของ Next.js เพื่อปรับแต่งโดยอัตโนมัติและเพิ่มประสิทธิภาพการโหลด คุณปฏิบัติ Next.js 15 Fullstack (App Router + Server Actions) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack (App Router + Server Actions) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack (App Router + Server Actions) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การปรับแต่งรูปภาพและแบบอักษร” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack (App Router + Server Actions) นี้ได้ไหม
ได้ บทเรียน Next.js 15 Fullstack (App Router + Server Actions) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การปรับแต่งรูปภาพและแบบอักษร
- การวิเคราะห์ขนาดชุดรวม
- กลยุทธ์การแคชชั้นสูง
- การสตรีมส่วนติดต่อผู้ใช้ด้วย Suspense และสถานะการโหลด