Просмотр исходного кода

update cache data when data is specified

Yuki Takei 3 лет назад
Родитель
Сommit
086989bb6c
1 измененных файлов с 9 добавлено и 5 удалено
  1. 9 5
      packages/app/src/stores/use-static-swr.tsx

+ 9 - 5
packages/app/src/stores/use-static-swr.tsx

@@ -1,6 +1,7 @@
 import assert from 'assert';
+
 import {
-  Key, SWRConfiguration, SWRResponse,
+  Key, SWRConfiguration, SWRResponse, useSWRConfig,
 } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
@@ -19,12 +20,15 @@ export function useStaticSWR<Data, Error>(
 
   assert.notStrictEqual(configuration?.fetcher, null, 'useStaticSWR does not support \'configuration.fetcher\'');
 
-  const swrResponse = useSWRImmutable(key, null, configuration);
+  const { cache } = useSWRConfig();
+  const swrResponse = useSWRImmutable(key, null, {
+    ...configuration,
+    fallbackData: configuration?.fallbackData ?? cache.get(key),
+  });
 
-  // mutate
+  // write data to cache directly
   if (data !== undefined) {
-    const { mutate } = swrResponse;
-    mutate(data);
+    cache.set(key, data);
   }
 
   return swrResponse;