Changeset View
Changeset View
Standalone View
Standalone View
landing/use-pre-load-image-assets.react.js
- This file was moved from landing/use-pre-load-assets.react.js.
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { type Asset } from './asset-meta-data.js'; | import { type ImageAsset } from './asset-meta-data.js'; | ||||
function usePreloadAssets(assets: Asset[]) { | function usePreloadImageAssets(assets: ImageAsset[]) { | ||||
React.useEffect(() => { | React.useEffect(() => { | ||||
const testWEBP = 'UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA'; | const testWEBP = 'UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA'; | ||||
const testImg = new Image(); | const testImg = new Image(); | ||||
// preload webp if supported | // preload webp if supported | ||||
testImg.onload = () => { | testImg.onload = () => { | ||||
for (const { url } of assets) { | for (const { url } of assets) { | ||||
const image = new Image(); | const image = new Image(); | ||||
image.src = `${url}.webp`; | image.src = `${url}.webp`; | ||||
} | } | ||||
}; | }; | ||||
// preload png if webp not supported | // preload png if webp not supported | ||||
testImg.onerror = () => { | testImg.onerror = () => { | ||||
for (const { url } of assets) { | for (const { url } of assets) { | ||||
const image = new Image(); | const image = new Image(); | ||||
image.src = `${url}.png`; | image.src = `${url}.png`; | ||||
} | } | ||||
}; | }; | ||||
testImg.src = `data:image/webp;base64,${testWEBP}`; | testImg.src = `data:image/webp;base64,${testWEBP}`; | ||||
}, [assets]); | }, [assets]); | ||||
} | } | ||||
export default usePreloadAssets; | export default usePreloadImageAssets; |