Browse Source

mutate with useLayoutEffect

Yuki Takei 2 years ago
parent
commit
73fe794fab

+ 5 - 1
apps/app/src/pages/_private-legacy-pages.page.tsx

@@ -6,6 +6,7 @@ import { useTranslation } from 'next-i18next';
 import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
+import { useIsomorphicLayoutEffect } from 'usehooks-ts';
 
 import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
 import type { CrowiRequest } from '~/interfaces/crowi-request';
@@ -49,7 +50,10 @@ const PrivateLegacyPage: NextPage<Props> = (props: Props) => {
 
   // clear the cache for the current page
   const { mutate } = useSWRxCurrentPage();
-  mutate(undefined, { revalidate: false });
+  useIsomorphicLayoutEffect(() => {
+    mutate(undefined, { revalidate: false });
+  }, []);
+
   useCurrentPageId(null);
   useCurrentPathname('/_private-legacy-pages');
 

+ 11 - 3
apps/app/src/pages/_search.page.tsx

@@ -4,7 +4,9 @@ import type { IUser } from '@growi/core';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 import { useTranslation } from 'next-i18next';
 import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
+import dynamic from 'next/dynamic';
 import Head from 'next/head';
+import { useIsomorphicLayoutEffect } from 'usehooks-ts';
 
 import SearchResultLayout from '~/components/Layout/SearchResultLayout';
 import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
@@ -16,8 +18,6 @@ import {
 } from '~/stores/context';
 import { useCurrentPageId, useSWRxCurrentPage } from '~/stores/page';
 
-import { SearchPage } from '../components/SearchPage';
-
 import type { NextPageWithLayout } from './_app.page';
 import type { CommonProps } from './utils/commons';
 import {
@@ -25,6 +25,9 @@ import {
 } from './utils/commons';
 
 
+const SearchPage = dynamic(() => import('../components/SearchPage').then(mod => mod.SearchPage), { ssr: false });
+
+
 type Props = CommonProps & {
   currentUser: IUser,
 
@@ -53,7 +56,10 @@ const SearchResultPage: NextPageWithLayout<Props> = (props: Props) => {
 
   // clear the cache for the current page
   const { mutate } = useSWRxCurrentPage();
-  mutate(undefined, { revalidate: false });
+  useIsomorphicLayoutEffect(() => {
+    mutate(undefined, { revalidate: false });
+  }, []);
+
   useCurrentPageId(null);
   useCurrentPathname('/_search');
 
@@ -74,6 +80,8 @@ const SearchResultPage: NextPageWithLayout<Props> = (props: Props) => {
 
   const title = generateCustomTitle(props, t('search_result.title'));
 
+  console.log('_search');
+
   return (
     <>
       <Head>

+ 5 - 1
apps/app/src/pages/me/[[...path]].page.tsx

@@ -8,6 +8,7 @@ import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
 import { useRouter } from 'next/router';
+import { useIsomorphicLayoutEffect } from 'usehooks-ts';
 
 import { BasicLayout } from '~/components/Layout/BasicLayout';
 import type { CrowiRequest } from '~/interfaces/crowi-request';
@@ -100,7 +101,10 @@ const MePage: NextPageWithLayout<Props> = (props: Props) => {
 
   // clear the cache for the current page
   const { mutate } = useSWRxCurrentPage();
-  mutate(undefined, { revalidate: false });
+  useIsomorphicLayoutEffect(() => {
+    mutate(undefined, { revalidate: false });
+  }, []);
+
   useCurrentPageId(null);
   useCurrentPathname('/me');
 

+ 7 - 1
apps/app/src/pages/tags.page.tsx

@@ -7,6 +7,7 @@ import { useTranslation } from 'next-i18next';
 import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
+import { useIsomorphicLayoutEffect } from 'usehooks-ts';
 
 import { LoadingSpinner } from '~/components/LoadingSpinner';
 import type { CrowiRequest } from '~/interfaces/crowi-request';
@@ -50,7 +51,10 @@ const TagPage: NextPageWithLayout<CommonProps> = (props: Props) => {
 
   // clear the cache for the current page
   const { mutate } = useSWRxCurrentPage();
-  mutate(undefined, { revalidate: false });
+  useIsomorphicLayoutEffect(() => {
+    mutate(undefined, { revalidate: false });
+  }, []);
+
   useCurrentPageId(null);
   useCurrentPathname('/tags');
 
@@ -77,6 +81,8 @@ const TagPage: NextPageWithLayout<CommonProps> = (props: Props) => {
 
   const title = generateCustomTitle(props, t('Tags'));
 
+  console.log('tags');
+
   return (
     <>
       <Head>

+ 4 - 1
apps/app/src/pages/trash.page.tsx

@@ -6,6 +6,7 @@ import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
+import { useIsomorphicLayoutEffect } from 'usehooks-ts';
 
 import { PagePathNavSticky } from '~/components/Common/PagePathNav';
 import type { CrowiRequest } from '~/interfaces/crowi-request';
@@ -44,7 +45,9 @@ const TrashPage: NextPageWithLayout<CommonProps> = (props: Props) => {
 
   // clear the cache for the current page
   const { mutate } = useSWRxCurrentPage();
-  mutate(undefined, { revalidate: false });
+  useIsomorphicLayoutEffect(() => {
+    mutate(undefined, { revalidate: false });
+  }, []);
 
   useGrowiCloudUri(props.growiCloudUri);