Skip to content

Commit d85e663

Browse files
authored
Merge pull request #1063 from equinor/fix/prefetch-img-state
Fix rich text prefetch
2 parents 44f780e + 71c2036 commit d85e663

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@equinor/amplify-component-lib",
3-
"version": "9.8.2",
3+
"version": "9.8.3",
44
"description": "Frontend Typescript components for the Amplify team",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/atoms/hooks/usePrefetchRichTextImages.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ export function usePrefetchRichTextImages({
1515
onImageRead,
1616
}: UsePrefetchRichTextImagesArgs) {
1717
const queryClient = useQueryClient();
18+
const startedPrefetch = useRef<string[]>([]);
1819
const prefetched = useRef<string[]>([]);
1920
const paths = richTextValues.flatMap((value) => getImagesFromRichText(value));
2021
const isPrefetching =
2122
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+
),
2328
}) > 0;
2429

2530
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({
3133
queryKey: [path],
3234
queryFn: async () => {
3335
const response = await onImageRead(path);
@@ -37,6 +39,13 @@ export function usePrefetchRichTextImages({
3739
staleTime: Infinity,
3840
gcTime: Infinity,
3941
});
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);
4049
}
4150
}, [onImageRead, paths, queryClient]);
4251

0 commit comments

Comments
 (0)