15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Next.js の Static HTML Export でも画像の最適化をしたい

Last updated at Posted at 2021-06-14

この記事は、現在開催中のイベント「3000文字Tips - 知ると便利なTipsをみんなへ届けよう」の記事です。

next/image は Static HTML Export では使えない

Next.js を使って静的サイトをつくるとき(next export)に、next/imageは使えない。

https://nextjs.org/docs/advanced-features/static-html-export より引用

The next/image component's default loader is not supported when using next export. However, other loader options will work.

静的サイトを生成する際に画像の最適化をしたい場合は、

  • next/image で別のローダーを使う
  • Gatsby のように Build 時に画像を最適化する

必要がある。

Gatsby のように Build 時に画像を最適化するには、以下のライブラリを使うと実現できる。

しかし、画像の最適化は Gatsby のように自動ではやってくれないため、自分で画像の最適化を指定する必要がある。
また、現状では画像のリサイズと WebP への変換を同時に行うことができない。(正確に言うと、responsive-loaderjimpを使う場合は使えない)

responsive-loadersharpを選択するとできるのだが、現在 sharp のアップデートに対して next-optimized-images の更新がされていないため、responsive-loaderに sharp を使えない。
PullRequest は飛んでいるようなので、対応待ちという状態。

早く PullRequest がマージされるといいですね。

15
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?