@@ -15,19 +15,21 @@ export function usePrefetchRichTextImages({
15
15
onImageRead,
16
16
} : UsePrefetchRichTextImagesArgs ) {
17
17
const queryClient = useQueryClient ( ) ;
18
+ const startedPrefetch = useRef < string [ ] > ( [ ] ) ;
18
19
const prefetched = useRef < string [ ] > ( [ ] ) ;
19
20
const paths = richTextValues . flatMap ( ( value ) => getImagesFromRichText ( value ) ) ;
20
21
const isPrefetching =
21
22
useIsFetching ( {
22
- predicate : ( query ) => paths . some ( ( path ) => query . queryKey [ 0 ] === path ) ,
23
+ predicate : ( query ) =>
24
+ startedPrefetch . current . some (
25
+ ( path ) =>
26
+ query . queryKey [ 0 ] === path && ! prefetched . current . includes ( path )
27
+ ) ,
23
28
} ) > 0 ;
24
29
25
30
useEffect ( ( ) => {
26
- for ( const path of paths . filter (
27
- ( path ) => ! prefetched . current . includes ( path )
28
- ) ) {
29
- prefetched . current . push ( path ) ;
30
- queryClient . prefetchQuery ( {
31
+ const handlePrefetch = async ( path : string ) => {
32
+ await queryClient . prefetchQuery ( {
31
33
queryKey : [ path ] ,
32
34
queryFn : async ( ) => {
33
35
const response = await onImageRead ( path ) ;
@@ -37,6 +39,13 @@ export function usePrefetchRichTextImages({
37
39
staleTime : Infinity ,
38
40
gcTime : Infinity ,
39
41
} ) ;
42
+ prefetched . current . push ( path ) ;
43
+ } ;
44
+ for ( const path of paths . filter (
45
+ ( path ) => ! startedPrefetch . current . includes ( path )
46
+ ) ) {
47
+ startedPrefetch . current . push ( path ) ;
48
+ handlePrefetch ( path ) ;
40
49
}
41
50
} , [ onImageRead , paths , queryClient ] ) ;
42
51
0 commit comments