Shun Miyazawa 3 лет назад
Родитель
Сommit
1040366bd1
1 измененных файлов с 7 добавлено и 6 удалено
  1. 7 6
      packages/remark-lsx/src/stores/lsx.tsx

+ 7 - 6
packages/remark-lsx/src/stores/lsx.tsx

@@ -96,11 +96,11 @@ type LsxResponse = {
 }
 
 const useSWRxLsxResponse = (
-    pagePath: string, options?: Record<string, string | undefined>, isImmutable?: boolean,
+    shouldFetch: boolean, pagePath: string, options?: Record<string, string | undefined>, isImmutable?: boolean,
 ): SWRResponse<LsxResponse, Error> => {
   return useSWR(
-    ['/_api/lsx', pagePath, options, isImmutable],
-    (endpoint, pagePath, options) => {
+    shouldFetch ? ['/_api/lsx', pagePath, options, isImmutable] : null,
+    (endpoint: string, pagePath, options) => {
       return axios.get(endpoint, {
         params: {
           pagePath,
@@ -121,13 +121,14 @@ type LsxNodeTree = {
   toppageViewersCount: number,
 }
 
-export const useSWRxNodeTree = (lsxContext: LsxContext, isImmutable?: boolean): SWRResponse<LsxNodeTree, Error> => {
-  const { data, error } = useSWRxLsxResponse(lsxContext.pagePath, lsxContext.options, isImmutable);
+export const useSWRxNodeTree = (shouldFetch: boolean, lsxContext: LsxContext, isImmutable?: boolean): SWRResponse<LsxNodeTree, Error> => {
+
+  const { data, error } = useSWRxLsxResponse(shouldFetch, lsxContext.pagePath, lsxContext.options, isImmutable);
 
   const isLoading = data === undefined && error == null;
 
   return useSWR(
-    !isLoading ? ['lsxNodeTree', lsxContext.pagePath, lsxContext.options, isImmutable, data, error] : null,
+    !isLoading && shouldFetch ? ['lsxNodeTree', lsxContext.pagePath, lsxContext.options, isImmutable, data, error] : null,
     (key, pagePath, options, isImmutable, data) => {
       if (data === undefined || error != null) {
         throw error;