Closed8

next/image の local images と remote images の使い分け

Taiga KIYOKAWATaiga KIYOKAWA

Remote Images

To use a remote image, the src property should be a URL string, which can be relative or absolute. Because Next.js does not have access to remote files during the build process, you'll need to provide the width, height and optional blurDataURL props manually:

import Image from 'next/image'

export default function Home() {
  return (
    <>
      <h1>My Homepage</h1>
      <Image
        src="/me.png"
        alt="Picture of the author"
        width={500}
        height={500}
      />
      <p>Welcome to my homepage!</p>
    </>
  )
}

https://nextjs.org/docs/basic-features/image-optimization#remote-images

Taiga KIYOKAWATaiga KIYOKAWA

Remote Images を使うタイミングとは? 基本的に Local Images で良くない?

Taiga KIYOKAWATaiga KIYOKAWA

next/image は Layout shift を防ぐためにサイズを確定するように要求してくる。もしサイズが確定できなければ img タグと同等の動きをする。

Taiga KIYOKAWATaiga KIYOKAWA

以下の3つのうちいずれか1つの方法でサイズを確定させる。

  1. Automatically, using a static import
  2. Explicitly, by including a width and height property
  3. Implicitly, by using fill which causes the image to expand to fill its parent element.
このスクラップは2023/05/29にクローズされました