Adjust image fallback cache behavior
This commit is contained in:
parent
b6a5a0c5da
commit
54c7b04d48
@ -5,7 +5,7 @@ import { BLUR_ENABLED } from '@/app/config';
|
|||||||
import { useAppState } from '@/state/AppState';
|
import { useAppState } from '@/state/AppState';
|
||||||
import { clsx} from 'clsx/lite';
|
import { clsx} from 'clsx/lite';
|
||||||
import Image, { ImageProps } from 'next/image';
|
import Image, { ImageProps } from 'next/image';
|
||||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
|
|
||||||
export default function ImageWithFallback(props: ImageProps & {
|
export default function ImageWithFallback(props: ImageProps & {
|
||||||
blurCompatibilityLevel?: 'none' | 'low' | 'high'
|
blurCompatibilityLevel?: 'none' | 'low' | 'high'
|
||||||
@ -22,7 +22,7 @@ export default function ImageWithFallback(props: ImageProps & {
|
|||||||
|
|
||||||
const { shouldDebugImageFallbacks } = useAppState();
|
const { shouldDebugImageFallbacks } = useAppState();
|
||||||
|
|
||||||
const [wasCached, setWasCached] = useState(true);
|
const [wasCached, setWasCached] = useState(false);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [didError, setDidError] = useState(false);
|
const [didError, setDidError] = useState(false);
|
||||||
|
|
||||||
@ -31,16 +31,6 @@ export default function ImageWithFallback(props: ImageProps & {
|
|||||||
|
|
||||||
const [hideFallback, setHideFallback] = useState(false);
|
const [hideFallback, setHideFallback] = useState(false);
|
||||||
|
|
||||||
const imgRef = useRef<HTMLImageElement>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const timeout = setTimeout(
|
|
||||||
() => setWasCached(imgRef.current?.complete ?? false),
|
|
||||||
100,
|
|
||||||
);
|
|
||||||
return () => clearTimeout(timeout);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoading && !didError) {
|
if (!isLoading && !didError) {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
@ -98,7 +88,7 @@ export default function ImageWithFallback(props: ImageProps & {
|
|||||||
</div>}
|
</div>}
|
||||||
<Image {...{
|
<Image {...{
|
||||||
...rest,
|
...rest,
|
||||||
ref: imgRef,
|
ref: (element) => setWasCached(element?.complete ?? false),
|
||||||
priority,
|
priority,
|
||||||
className: classNameImage,
|
className: classNameImage,
|
||||||
onLoad,
|
onLoad,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user