Taichi Masuyama 4 лет назад
Родитель
Сommit
828db5848e

+ 1 - 1
packages/app/src/stores/context.tsx

@@ -4,5 +4,5 @@ import { useStaticSWR } from './use-static-swr';
 import { TargetAndAncestors } from '../interfaces/page-listing-results';
 import { TargetAndAncestors } from '../interfaces/page-listing-results';
 
 
 export const useTargetAndAncestors = (initialData?: TargetAndAncestors): SWRResponse<TargetAndAncestors, Error> => {
 export const useTargetAndAncestors = (initialData?: TargetAndAncestors): SWRResponse<TargetAndAncestors, Error> => {
-  return useStaticSWR<TargetAndAncestors, Error>('swr-targetAndAncestors', initialData);
+  return useStaticSWR<TargetAndAncestors, Error>('targetAndAncestors', initialData);
 };
 };

+ 1 - 0
packages/app/src/stores/page-listing.tsx

@@ -14,5 +14,6 @@ export const useSWRxPageAncestorsChildren = (
         ancestorsChildren: response.data.ancestorsChildren,
         ancestorsChildren: response.data.ancestorsChildren,
       };
       };
     }),
     }),
+    { revalidateOnFocus: false },
   );
   );
 };
 };

+ 19 - 20
packages/app/src/stores/use-static-swr.tsx

@@ -1,27 +1,26 @@
-import useSWR, {
-  Key, SWRResponse, mutate, useSWRConfig,
+import {
+  Key, SWRConfiguration, SWRResponse, mutate,
 } from 'swr';
 } from 'swr';
+import useSWRImmutable from 'swr/immutable';
 import { Fetcher } from 'swr/dist/types';
 import { Fetcher } from 'swr/dist/types';
 
 
 
 
-export const useStaticSWR = <Data, Error>(
-  key: Key,
-  initialData?: Data | Fetcher<Data>,
-  updateData?: Data | Fetcher<Data>,
-): SWRResponse<Data, Error> => {
-  const { cache } = useSWRConfig();
+export function useStaticSWR<Data, Error>(key: Key): SWRResponse<Data, Error>;
+export function useStaticSWR<Data, Error>(key: Key, data: Data | Fetcher<Data> | null): SWRResponse<Data, Error>;
+export function useStaticSWR<Data, Error>(key: Key, data: Data | Fetcher<Data> | null,
+  configuration: SWRConfiguration<Data, Error> | undefined): SWRResponse<Data, Error>;
 
 
-  if (updateData == null) {
-    if (cache.get(key) == null && initialData != null) {
-      mutate(key, initialData, false);
-    }
-  }
-  else {
-    mutate(key, updateData);
+export function useStaticSWR<Data, Error>(
+    ...args: readonly [Key]
+    | readonly [Key, Data | Fetcher<Data> | null]
+    | readonly [Key, Data | Fetcher<Data> | null, SWRConfiguration<Data, Error> | undefined]
+): SWRResponse<Data, Error> {
+  const [key, fetcher, configuration] = args;
+
+  const fetcherFixed = fetcher || configuration?.fetcher;
+  if (fetcherFixed != null) {
+    mutate(key, fetcherFixed);
   }
   }
 
 
-  return useSWR(key, null, {
-    revalidateOnFocus: false,
-    revalidateOnReconnect: false,
-  });
-};
+  return useSWRImmutable(key, null, configuration);
+}

+ 0 - 2
packages/app/src/utils/swr-utils.ts

@@ -2,7 +2,5 @@ import { SWRConfiguration } from 'swr';
 
 
 
 
 export const swrGlobalConfiguration: SWRConfiguration = {
 export const swrGlobalConfiguration: SWRConfiguration = {
-  fetcher: undefined,
-  revalidateOnFocus: false,
   errorRetryCount: 1,
   errorRetryCount: 1,
 };
 };