2
2

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 5 years have passed since last update.

react-helmetで画像をpreloadする

2
Last updated at Posted at 2019-04-21

メモ書きです。

画像を先に読み込みたい時について。

以下のようなコンポーネントを作る。

import * as React from 'react'
import { Helmet } from 'react-helmet'

interface ImagePreloaderProps {
  images: string[]
}

const ImagePreloader: React.FunctionComponent<ImagePreloaderProps> = ({ images }) => (
  <Helmet>
    {images
      .filter(item => item)
      .map((image, index) => (
        <link rel="preload" href={image} as="image"/>
      ))}
  </Helmet>
)

export default ImagePreloader

今やっているやつが、スライドを見せていくような感じのもので、そのスライド中に含まれる画像の読み込みが通信環境によって遅い時があった。

予めそのスライドのコンテナとなるコンポーネントで、このコンポーネントを呼び出すことでpreloadすることができた。

通信環境の再現はchromeのデベロッパーツール:Networkタブでできる。 Slow 3G 状態でも問題なく読み込めることを確認できた。

2
2
2

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?