Optymalizacja obrazów i czcionek
Optymalizuj obrazy za pomocą `next/image` i efektywnie zarządzaj czcionkami, aby skrócić czas ładowania.
Optymalizacja obrazów i czcionek to bezpłatna lekcja Next.js 15 Fullstack Web Apps na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Next.js 15 Fullstack Web Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Optimize Images & Fonts?
Optimizing images and fonts is crucial for a fast-loading website. Large files slow down your page, impacting user experience and SEO.
- Faster Loading: Users don't wait for slow pages.
- Improved SEO: Search engines favor faster sites.
- Better UX: Smooth, responsive feel.
- Lower Bandwidth: Saves data for users.
Next.js Image Component
Next.js provides a powerful next/image component. It automatically optimizes images for you, handling responsiveness, lazy loading, and format conversion to modern formats like WebP.
Using next/image ensures your images are served in the most efficient way possible, often without you needing to do manual optimization.
Using the Image Component
To use next/image, import it and then define your image's src, alt text, width, and height. The width and height are crucial for preventing layout shifts (CLS).
import Image from 'next/image';
export default function MyImage() {
return (
<Image
src="/my-photo.jpg"
alt="A descriptive alt text"
width={500}
height={300}
/>
);
}Layout Modes & Responsiveness
next/image offers different layout modes to control how images scale:
intrinsic(default): Scales down for smaller viewports, but doesn't scale up beyond its original size.fixed: Image is fixed width and height.fill: Image fills its parent element, useful for background images or when the image dimensions are unknown. Requires the parent to haveposition: relative.
Loading Priority with `priority`
For images above the fold (visible when the page first loads), you should add the priority prop. This tells Next.js to preload the image, improving your site's Largest Contentful Paint (LCP) metric.
Only use priority for critical images, like hero banners. Overusing it can negate its benefits.
import Image from 'next/image';
export default function HeroSection() {
return (
<Image
src="/hero-banner.jpg"
alt="Hero banner for the website"
width={1200}
height={600}
priority
/>
);
}The Need for Font Optimization
Custom fonts can significantly impact performance. Large font files mean longer download times, leading to 'flash of unstyled text' (FOUT) or 'flash of invisible text' (FOIT).
Optimizing fonts reduces file size and ensures they load smoothly, improving the visual experience and preventing layout shifts.
Google Fonts with `next/font`
Next.js 15 simplifies using Google Fonts with next/font/google. It automatically self-hosts fonts, reduces layout shifts (CLS), and improves privacy by avoiding direct requests to Google.
Import your desired font, then apply it to your HTML elements.
import { Inter } from 'next/font/google';
// Configure the Inter font
const inter = Inter({ subsets: ['latin'] });
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.className}>
<Component {...pageProps} />
</main>
);
}Self-Hosting Local Fonts
For self-hosted fonts (fonts you provide yourself), use next/font/local. This allows you to bundle your font files directly with your application, giving you full control over their delivery.
Define your font source and apply it just like Google Fonts.
import localFont from 'next/font/local';
// Configure a local custom font
const myCustomFont = localFont({ src: './my-custom-font.woff2' });
export default function MyPage() {
return (
<h1 className={myCustomFont.className}>
My Custom Title
</h1>
);
}Font Loading Strategies
To further optimize font loading:
display: swap: This CSS property shows fallback text immediately while the custom font loads, preventing 'flash of invisible text' (FOIT).next/fonthandles this by default.- Subset Fonts: Only include the characters you need from a font to reduce file size.
- Preload Critical Fonts: For your most important fonts, consider preloading them using
<link rel="preload">in your document's head.
Image Optimization Check
Which of the following are benefits of using the next/image component in a Next.js application?
Recap: Optimized for Performance
Great job! You've learned how to significantly boost your Next.js app's performance by optimizing images and fonts.
- Use
next/imagefor automatic image optimization, lazy loading, and layout shift prevention. - Leverage the
priorityprop for critical above-the-fold images. - Utilize
next/font/googleandnext/font/localfor efficient, self-hosted font loading, which also prevents layout shifts.
These practices lead to faster, more user-friendly applications.
Często zadawane pytania
Czy lekcja „Optymalizacja obrazów i czcionek” jest bezpłatna?
Tak — pełny tekst „Optymalizacja obrazów i czcionek” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Next.js 15 Fullstack Web Apps, przejdź na CoddyKit PRO. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Optymalizacja obrazów i czcionek”?
Optymalizuj obrazy za pomocą `next/image` i efektywnie zarządzaj czcionkami, aby skrócić czas ładowania. Ćwiczysz Next.js 15 Fullstack Web Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Next.js 15 Fullstack Web Apps?
Nie wymagamy żadnego doświadczenia. Next.js 15 Fullstack Web Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Optymalizacja obrazów i czcionek”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Next.js 15 Fullstack Web Apps?
Tak. Każda lekcja Next.js 15 Fullstack Web Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wdrażanie na Vercel i Netlify
- Optymalizacja obrazów i czcionek
- Analiza bundla i audyty wydajności
- Core Web Vitals i monitorowanie