0Pricing
Next.js 15 Fullstack Web Apps · レッスン

画像とフォントの最適化

`next/image`で画像を最適化し、フォントを効率的に管理して読み込み時間を短縮します。

「画像とフォントの最適化」はCoddyKit上の無料Next.js 15 Fullstack Web Appsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNext.js 15 Fullstack Web Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 have position: 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/font handles 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/image for automatic image optimization, lazy loading, and layout shift prevention.
  • Leverage the priority prop for critical above-the-fold images.
  • Utilize next/font/google and next/font/local for efficient, self-hosted font loading, which also prevents layout shifts.

These practices lead to faster, more user-friendly applications.

よくある質問

「画像とフォントの最適化」レッスンは無料ですか?

はい。「画像とフォントの最適化」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Next.js 15 Fullstack Web Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。

「画像とフォントの最適化」で何を学びますか?

`next/image`で画像を最適化し、フォントを効率的に管理して読み込み時間を短縮します。 ブラウザで直接実行するハンズオンコードでNext.js 15 Fullstack Web Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Next.js 15 Fullstack Web Appsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのNext.js 15 Fullstack Web Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「画像とフォントの最適化」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このNext.js 15 Fullstack Web Appsレッスンでコードを書いて実行できますか?

はい。すべてのNext.js 15 Fullstack Web Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. VercelとNetlifyへのデプロイ
  2. 画像とフォントの最適化
  3. バンドル分析とパフォーマンス監査
  4. Core Web Vitalsとモニタリング
← Next.js 15 Fullstack Web Appsに戻る