Using Images in Next.js (next/image examples)

Lee Robinson
25 Jun 202309:10

Summary

TLDRこの動画スクリプトでは、Web上の画像使用における3つの一般的な問題とその解決策について説明されています。レイアウトシフト、ビューポートやデバイスに最適化されていない大きな画像の読み込み、さらに画像表示と希望のレイアウトを実現するのが困難である点に焦点を当てています。Image component index.jsがこれらの問題を解決するのに役立つことが期待されています。 動画では、基本的な使用方法から、次のような複数のシーンを紹介しています。altタグ、ソース画像を設定し、画面全体を占めるスタイルを適用した画像コンポーネントの定義方法や、画像の最適化と縮小、外部ドメインからの画像の取り扱い、背景画像の設定、グリッドパターンでの複数画像使用、レスポンシブな画像表示など、幅広い内容が含まれています。 また、カスタマイズ性の高いAPI参照、画像最適化サービスの変更方法など、より詳細な情報はドキュメントで確認できると案内しています。

Takeaways

  • 🌐 ウェブでの画像使用に関連する3つの一般的な問題: レイアウトシフト、过大な画像の読み込み、最適化されていないビューポート対応。
  • 🚀 Next.jsのImageコンポーネントは、これらの問題を解決し、開発者の体験を向上させることを目指しています。
  • 🖼️ Imageコンポーネントは、altタグ、ソース画像、そしてスタイルを引き受け、自動的に幅と高さを設定してレイアウトシフトを防ぎます。
  • 📏 画像の最適化: ネクスト.jsは自動的に画像を最適化し、より軽量なavifまたはwebp形式で提供します。
  • 🔒 外部ドメインからの画像使用時は、許可されたドメインのホワイトリストを定義する必要があります。
  • 🎨 背景画像を設定する場合、fillプロパティを使用して親要素の全体幅を占めることができます。
  • 📊 sizesプロパティを使用して、ビューポートの幅に基づいて最適な画像サイズを自動的に選択できます。
  • 👌 品質プロパティを使用して、画像の品質を調整できます。100%に設定すると、画像の品質が向上します。
  • 🌀 ブラウザのUXを向上させるために、blur upプレースホルダーを自動生成することができます。
  • 🔧 CSSのobject-fitプロパティを使用して、画像の表示方法(例: cover)を調整できます。
  • 🔄 グリッドパターンを使用して、fillプロパティとobject-fit coverを組み合わせて、响应式な画像レイアウトを作成できます。
  • 📈 ネクスト.jsのAPIリファレンスを使用して、画像コンポーネントの設定を調整できます。また、ローダーを使用して異なるサービスで画像を最適化できます。

Q & A

  • ウェブ上で画像を扱う際に直面する一般的な問題は何ですか?

    -ウェブ上で画像を扱う際に直面する一般的な問題は3つあります。1つは、画像の読み込みによるレイアウトのシフトが分散访客に干扰を与えることです。2つ目は、ページや訪問者が使用するデバイスを最適化していない大きすぎる画像を読み込むことです。3つ目は、実際に画面に画像を表示し、望むレイアウトを実現するのが非常に困難であることです。

  • 画像コンポーネントindex.jsはどのようにこれらの問題を解決する予定ですか?

    -画像コンポーネントindex.jsは、画像の読み込みによるレイアウトシフトを防ぐために自動的に幅と高さを設定し、最適化された画像を提供することで、これらの問題を解決する予定です。また、開発者が画像をよりスムーズに使用できるようにする機能も含まれています。

  • 画像コンポーネントを使用する際にALTタグとソース画像は何为其重要ですか?

    -ALTタグは画像の代替テキストを提供し、アクセシビリティを向上させます。ソース画像は実際に表示される画像を指定します。これらは、画像コンポーネントが正しく機能し、ユーザーが画像を理解できるようにするために重要です。

  • 画像を最適化するために使用されるフォーマットとしてavifやwebpは何ですか?

    -avifやwebpは、より小さいファイルサイズの画像を提供できる近代的な画像フォーマットです。これにより、高速な読み込みと高品質な画像表示が可能になり、ウェブサイトのパフォーマンスが向上します。

  • 外部からの画像を最適化するにはどうすればいいですか?

    -外部からの画像を最適化するには、next.jsの設定ファイルで許可されるドメインのホワイトリストを定義する必要があります。また、画像の幅と高さを正確に指定する必要があります。

  • fillプロパティはどのように動作しますか?

    -fillプロパティは、画像を親要素の全体幅に広がるように設定します。これにより、幅と高さを手動で設定する必要がなくなり、自動的にレイアウトが調整されます。

  • sizesプロパティは何のために使用されますか?

    -sizesプロパティは、ビューポートの幅に基づいて最適な画像サイズを自動的に選択するために使用されます。これにより、訪問者のデバイスやビューポートに合わせて適切なサイズの画像が提供されます。

  • 画像コンポーネントでオブジェクトフィットカバーを使用する好处は何ですか?

    -オブジェクトフィットカバーを使用すると、画像が親要素のサイズに合わせて調整され、背景として表示される際に適切にフィットします。これにより、画像がビューポートに合わせてリサイズされる際に、見た目が維持されます。

  • CSSグリッドパターンで画像を表示する際に重要な要素は何ですか?

    -CSSグリッドパターンで画像を表示する際に重要な要素は、親要素がCSSグリッドであることと、画像コンポーネントにfillプロパティが設定されていることです。これにより、画像がグリッドのサイズに合わせて自動的に調整され、リponsiveなデザインを実現できます。

  • 画像コンポーネントが自動的にレスポンシブになるのはなぜですか?

    -画像コンポーネントは、ソースサイズとビューポートの幅に基づいて自動的に最適なサイズの画像を提供するため、自動的にレスポンシブになります。これにより、訪問者のデバイスやビューポートのサイズに応じて、最適な表示結果が得られます。

  • 画像コンポーネントの設定をカスタマイズするにはどうすればいいですか?

    -画像コンポーネントの設定をカスタマイズするには、ドキュメントにあるAPI参照を利用することができます。また、ローダーを通じて異なるサービスを使用して画像を最適化することもできます。

Outlines

00:00

🖼️ ウェブイメージ最適化の問題と解決策

第1段落では、ウェブページで画像を使用する際に直面する一般的な3つの問題について説明されています。1つは、画像の読み込みによるレイアウトシフトがユーザーに干扰を与えることです。2つ目は、ページや訪問者のデバイスに最適化されていない大きすぎる画像を読み込むことです。3つ目は、望むレイアウトを実際に画面に表示するのが難しいことです。これらの問題を解決するための方法として、image component index.jsが提案されています。

05:02

🛠️ ネクスト..jsの画像コンポーネントの使い方

第2段落では、ネクスト..jsの画像コンポーネントを使って、異なる方法で画像を表示する例について詳しく説明されています。基本的な使用方法から、背景画像を設定する方法、グリッドパターンでの複数画像の表示方法まで、様々な例が紹介されています。また、画像を最適化するためのallow listの設定や、画像フォーマットの指定方法なども説明されています。最後に、画像コンポーネントのAPIリファレンスについて触れられています。

Mindmap

Keywords

💡レイアウトシフト

レイアウトシフトは、ウェブページの要素が読み込まれた時や再flowsしされた時に生じる表示上の不整合を指します。このビデオスクリプトでは、画像の読み込みによるレイアウトシフトを防ぐために、画像の幅と高さを適切に設定することが重要だと述べられています。

💡オプティマイズ

オプティマイズとは、ウェブページに最適な表示を提供するために画像のサイズやフォーマットを調整することを指します。スクリプトでは、Next.jsが画像を自動的にオプティマイズし、より小さいファイルサイズにすることで高速な読み込みと良いユーザー体験を提供できることが説明されています。

💡ビューポート最適化

ビューポート最適化は、ウェブページの画像がデバイスの画面サイズに合わせて自動的に調整されることを意味します。これにより、さまざまなデバイスで最適な表示が保証されます。スクリプトでは、Next.jsがビューポートの幅に基づいて画像のサイズを自動調整することが挙げられています。

💡リモートイメージ

リモートイメージは、ウェブページから直接参照されるがリポジトリに含まれていない画像を指します。スクリプトでは、Next.jsが外部ドメインからの画像をオプティマイズできるように、特定のドメインを許可リストに追加する方法が説明されています。

💡背景画像

背景画像とは、ウェブページの背景に設定される画像のことを指します。スクリプトでは、Next.jsの画像コンポーネントを使用して背景画像を設定し、`fill`プロパティを用いて親要素の全体に広がることができます。

💡CSS Grid

CSS Gridは、ウェブページのレイアウトを構築するためのCSSの機能です。スクリプトでは、CSS Gridを使用して画像をグリッドパターンに配置し、リサイズや响应性の高いデザインを実現する方法が紹介されています。

💡オブジェクトフィット

オブジェクトフィットは、画像を要素内にフィットさせるCSSのプロパティです。`cover`や`contain`などの値を用いて、画像が要素内に適切に表示されるように調整できます。スクリプトでは、`object fit cover`を使用して画像が背景にフィットするように設定する方法が説明されています。

💡レスポンシブ画像

レスポンシブ画像は、画面サイズやデバイスによって自動的に調整される画像です。スクリプトでは、Next.jsの画像コンポーネントがデフォルトでレスポンシブであり、ビューポートの変更に応じて自動的に調整されることが挙げられています。

💡画像コンポーネント

画像コンポーネントは、Next.jsにおいて画像を扱うための特別なコンポーネントです。スクリプトでは、画像コンポーネントを使用して、画像の読み込み、表示、および最適化を簡単に管理できることが説明されています。

💡Next.js

Next.jsは、Reactベースのサーバーサイドレンダリングフレームワークです。スクリプトでは、Next.jsが提供する機能を利用して、画像の最適化やレスポンシブデザインを実現する方法が紹介されています。

Highlights

Three common problems when working with images on the web are layout shift, large images not optimized for the viewport or device, and difficulty in getting images on the screen with the desired layout.

The image component in Next.js aims to solve these problems by providing a better developer experience for using images.

The basic image component takes an ALT tag for accessibility, a source image, and styles to fit the screen width, automatically setting width and height to prevent layout shift.

Next.js automatically optimizes images, making them smaller without manual intervention by using modern file formats like AVIF or WebP.

The optimized image file size is significantly reduced, for example, from 30.3 kilobytes to 7.2 kilobytes.

Remote images can also be optimized within Next.js, but an explicit allowlist of domains must be defined for security and control.

The image component in Next.js supports dynamic images from external sources with the right configuration.

The fill prop allows an image to take up the entire width of its parent element, replacing the older layout prop.

The sizes prop, used with fill, instructs Next.js to serve different image variations based on the viewport and device.

The quality prop can be adjusted for images, with 100% being an option for higher quality at the cost of larger file sizes.

Next.js can generate a blur-up placeholder for local images, improving user experience.

The style prop allows for CSS adjustments like object-fit without the need for additional class names.

Using the fill layout with CSS Grid and object-fit cover creates a responsive and visually appealing grid pattern for images.

The image component is responsive by default, adjusting to the viewport size and ensuring images display full width across devices.

Next.js provides a full API reference for the image component, including the ability to change the image optimization service through loaders.

The video offers practical examples of using the Next.js image component, showcasing its versatility and ease of implementation.

Transcripts

play00:00

if you're working with images on the web

play00:01

you might have noticed three common

play00:03

problems the first is that there's

play00:05

layout shipped from your images loading

play00:07

in which is distracting for your

play00:08

visitors two is that you load images

play00:11

that are too large for your page so they

play00:13

don't get optimized for the viewport or

play00:15

for the device that the visitor is using

play00:18

and the third is that the experience of

play00:20

actually getting the images on the

play00:22

screen and the layout that you want is

play00:24

actually pretty difficult to do so you

play00:25

want to have a little bit better

play00:26

developer experience for using images

play00:28

well the image component index.js is

play00:31

hopefully here to solve those problems

play00:33

for you okay so in my editor I have the

play00:35

nexgs image component examples from the

play00:37

documentation running locally and we're

play00:40

going to walk through a few different

play00:41

examples of using the image component so

play00:43

let's start with the most basic I have

play00:46

this image component defined on the left

play00:47

it takes an ALT tag for accessibility

play00:51

some Source image and then I also passed

play00:54

in some styles to make it take up the

play00:55

full width of the screen now the source

play00:58

is coming from up top or actually

play01:00

importing the image file from the public

play01:02

directory now this is nice because we

play01:06

don't have to manually set the width or

play01:08

height setting the width and height is

play01:10

important because this is how you

play01:11

prevent layout shift so when you import

play01:13

an image here it's going to

play01:15

automatically do that for you so now

play01:17

let's go look at the browser and see

play01:18

what's actually happening here so on

play01:20

localhost 3000 I have Dev tools opened

play01:23

here and I see an image tag so there's

play01:26

no wrapping elements it's just the

play01:28

native web image tag in previous

play01:29

versions of nexgs we had to add wrapping

play01:32

elements to

play01:34

help support the features that were not

play01:36

cross browser yet in terms of aspect

play01:38

ratios and styling but now in the latest

play01:41

version of next.js there's no wrapping

play01:43

elements so we have an image tag we see

play01:45

our alt tag we see some properties that

play01:47

we've automatically defined for you like

play01:49

the width and the hype to prevent that

play01:51

layout shift and then we see this

play01:53

interesting URL now this URL is the

play01:56

automatically optimized image for us so

play01:58

let's take a quick detour here and I'll

play02:00

go look at this file Slash pursell.png

play02:03

from the public directory that we're

play02:05

serving up now if we look down here we

play02:07

see that the original size of This Is

play02:09

30.3 kilobytes this is a PNG file and

play02:13

I've done a hard reload here so it's not

play02:15

being cached

play02:17

but this underscore next slash image

play02:20

route is what's automatically optimizing

play02:23

that image and making it a bit smaller

play02:25

so the same image passed in through that

play02:27

route we look here and we see that it

play02:29

detects that because I'm using Chrome we

play02:32

can actually use a more modern file

play02:35

extension so we're going to use the

play02:36

format of avif or webp which are going

play02:39

to help you get even smaller file sizes

play02:41

so we see that down below here with

play02:43

content type and if I close this out we

play02:46

see that the size of this one is 7.2

play02:49

kilobytes so again I'll do a hard reload

play02:51

here and we see 7.2 kilobytes that's

play02:53

quite a bit smaller and you know 30

play02:55

kilobytes was already a decently small

play02:56

image this gets even more impactful when

play02:58

you talk about megabyte sized images or

play03:01

really high quality images but what if

play03:03

my images aren't in my repository

play03:05

they're from some remote location well

play03:07

that's okay too let's take a look at

play03:09

another example so in my left to have

play03:11

another image component with an ALT tag

play03:13

and a source and a width and a height

play03:15

and the max width but notice that the

play03:18

source is a URL that some external image

play03:21

now this also works inside of next.js

play03:23

but we have to Define an explicit allow

play03:25

list of the domains that we want to be

play03:28

able to optimize images from so if we

play03:30

hop over to our next config we see that

play03:33

not only have we defined the image

play03:34

formats that we want to support which

play03:36

are avif and webp we also can Define

play03:39

some patterns that we want to match for

play03:41

the URLs inside of our source so we're

play03:44

saying we're going to allow all of the

play03:46

images from this

play03:47

assets.presell.com subdomain and

play03:50

specifically for this path name image

play03:52

slash upload slash something now this is

play03:55

important because it makes sure that you

play03:57

lock down the URLs that people can use

play03:59

your next.js server to optimize images

play04:01

with it's important to note here that we

play04:04

still had to define the width and the

play04:06

height of the image now this can be a

play04:08

little bit tricky sometimes if the width

play04:10

or the height are really Dynamic but

play04:12

it's really important because this is

play04:14

the only way that you can go without

play04:16

having layout shift from your image

play04:18

loading in okay let's look at another

play04:20

example of setting a background image

play04:22

for the entire page so on my left I have

play04:25

another image tag it has an alt and a

play04:28

source just like before but there's a

play04:29

couple new things here we want to dig

play04:31

into the first I'm going to talk about

play04:32

which is the most important is fill so

play04:35

rather than setting a width and height

play04:37

of this image we wanted to take up the

play04:39

entire width of our parent and we can do

play04:42

that by specifying this fill prop now

play04:45

previously in older versions of nexjs

play04:47

there was a layout prop that had a few

play04:49

different options but layout has been

play04:51

replaced with different options

play04:52

including this fill prop so go check out

play04:54

the documentation if you want to see

play04:56

those new options and secondly we Define

play04:59

this sizes prop so we're telling next JS

play05:02

and I'll actually pull up these here

play05:03

because there's quite a bit of details

play05:05

here on sizes but it's really important

play05:07

that if you're using fill you should

play05:09

also use sizes the most important part

play05:11

here is if the sizes property includes

play05:14

something like 50 view width or 100 view

play05:16

width in this instance which is a

play05:18

percentage of the viewport width The

play05:19

Source set or the number of images is

play05:22

trimmed down to not include any values

play05:24

which are too small to ever be necessary

play05:27

so you get a really configure the

play05:29

different variations of the image that

play05:31

you want next.js to automatically serve

play05:34

up depending on the viewport and the

play05:36

device of your visitor now there's a

play05:38

couple other cool things here one

play05:40

there's this optional quality prop it

play05:42

defaults to 75 I believe percent but you

play05:44

can optionally do 100 if you would like

play05:46

it's going to be a slightly bigger image

play05:48

but for a background image that might be

play05:50

nice to have you can have a nice blur up

play05:52

placeholder which is really easy to do

play05:54

when you're importing a local image that

play05:56

local image nextges is going to be able

play05:58

to automatically generate the blur data

play06:01

URL which gives you that nice blur up

play06:02

placeholder and has a better ux and the

play06:06

last thing here is a style prop so this

play06:09

is just using CSS you could do this

play06:11

through a class name if you wanted to

play06:12

too but we have object fit cover we

play06:15

could also do something like contain in

play06:17

we could do something like none I think

play06:20

is another one you could have a couple

play06:22

different options here on how you want

play06:24

to

play06:24

actually lay this out but I typically

play06:26

use cover for most of these things let's

play06:28

look at another example of using

play06:31

multiple images with layout fill in a

play06:33

grid pattern so on the left this image

play06:36

looks pretty similar to the last one it

play06:38

has fill it has some sizes and then I'm

play06:41

also using object fit cover but you'll

play06:44

notice that I've specified a relative

play06:46

parent around it which is very important

play06:48

and a height now the parents of this

play06:52

element is a CSS Grid it's got a little

play06:54

bit of spacing between the items we've

play06:56

got some CSS magic of Auto fitting the

play06:59

sizes so what happens here if I take

play07:01

this element of the mountain image and

play07:04

we add some more images to the grid so

play07:06

let's do

play07:08

let's do maybe six images here

play07:10

so now what you'll see is that these

play07:13

images are going to have this relative

play07:15

position of their wrapping component and

play07:18

it's going to have a height of 400

play07:19

pixels and this value let me just

play07:22

resize here a little bit

play07:24

you get this really nice effect from CSS

play07:27

grid where the combination of object fit

play07:31

cover

play07:32

means that this scales really well and

play07:34

it can be responsive thanks to CSS grid

play07:37

as well too so this is a really nice

play07:39

pattern that I like to use okay the last

play07:41

one I want to talk about is responsive

play07:43

images which the great thing about the

play07:45

image component is that they're

play07:46

responsive by default so on the left

play07:48

again I have an ALT tag I have a source

play07:51

sizes and then I'm making the image

play07:53

display full width but now what's really

play07:55

cool is that by default what I resize

play07:58

the viewport the image component is

play08:00

automatically responsive so that's

play08:02

really really nice and because I have a

play08:05

flex parrot here I could also do

play08:07

something like let's say I want

play08:10

hello world another item I can display

play08:13

that down below and I could have some

play08:15

kind of card layout for example and I

play08:17

can just use this really really nicely

play08:19

now if you want to fine tune or tweak

play08:21

any of the knobs on the image component

play08:22

we have a full API reference in our

play08:25

documentation including the ability to

play08:27

change what service actually optimizes

play08:29

your images through loaders so by

play08:32

default and it works whether you

play08:33

self-host or you use a managed service

play08:35

you can optimize images on the next.js

play08:38

server now if you for example want to

play08:41

use a different service to optimize your

play08:42

images you can specify a loader that

play08:44

will make this very easy for you so if

play08:47

you look at loader it takes in a

play08:48

function this can Define the URL for any

play08:52

service and you can pass in things like

play08:53

the source the width and the quality and

play08:56

it makes it really easy to swap out for

play08:58

whichever service you prefer okay that's

play09:00

all for this video hopefully you found

play09:02

this helpful to see some practical

play09:03

examples of how to use the image

play09:05

component stay tuned for the next one

play09:06

and let me know in the comments what

play09:08

you'd like to see peace

Rate This

5.0 / 5 (0 votes)

Related Tags
ウェブ最適化画像コンポーネント次世代ウェブ開発者体験レイアウトシフトビューポート最適化外部イメージリモートソースCSSグリッドレスポンシブデザイン
Do you need a summary in English?