Shun Miyazawa 3 лет назад
Родитель
Сommit
4c794a42a0
2 измененных файлов с 20 добавлено и 14 удалено
  1. 5 7
      packages/app/src/pages/[[...path]].page.tsx
  2. 15 7
      packages/app/src/stores/context.tsx

+ 5 - 7
packages/app/src/pages/[[...path]].page.tsx

@@ -57,14 +57,14 @@ import DisplaySwitcher from '../components/Page/DisplaySwitcher';
 // import PageStatusAlert from '../client/js/components/PageStatusAlert';
 import {
   useCurrentUser,
-  useIsLatestRevision, useCurrentRevisionId,
+  useIsLatestRevision, useCurrentRevisionId, useRevisionBody,
   useIsForbidden, useIsNotFound, useIsSharedUser,
   useIsEnabledStaleNotification, useIsIdenticalPath,
   useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
   useDrawioUri, useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
   useIsAclEnabled, useIsSearchPage, useTemplateTagData, useTemplateBodyData, useIsEnabledAttachTitleHeader,
   useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPageId, useCurrentPathname,
-  useIsSlackConfigured, useRendererConfig, useEditingMarkdown,
+  useIsSlackConfigured, useRendererConfig,
   useEditorConfig, useIsAllReplyShown, useIsUploadableFile, useIsUploadableImage, useCustomizedLogoSrc, useIsContainerFluid,
 } from '../stores/context';
 
@@ -255,7 +255,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
 
   const { data: currentPage } = useSWRxCurrentPage(undefined, pageWithMeta?.data ?? null); // store initial data
 
-  useEditingMarkdown(pageWithMeta?.data.revision?.body);
+  useRevisionBody(pageWithMeta?.data.revision?.body);
 
   const { data: grantData } = useSWRxIsGrantNormalized(pageId);
   const { mutate: mutateSelectedGrant } = useSelectedGrant();
@@ -467,20 +467,18 @@ async function injectRoutingInformation(context: GetServerSidePropsContext, prop
 
   const page = props.pageWithMeta?.data;
 
+  props.isNotFound = !(page != null && !page.isEmpty);
+
   if (props.isIdenticalPathPage) {
     // TBD
-    props.isNotFound = false;
   }
   else if (page == null) {
-    props.isNotFound = true;
     props.isNotCreatablePage = !isCreatablePage(currentPathname);
     // check the page is forbidden or just does not exist.
     const count = isPermalink ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname });
     props.isForbidden = count > 0;
   }
   else {
-    props.isNotFound = page.isEmpty;
-
     // /62a88db47fed8b2d94f30000 ==> /path/to/page
     if (isPermalink && page.isEmpty) {
       props.currentPathname = page.path;

+ 15 - 7
packages/app/src/stores/context.tsx

@@ -186,6 +186,10 @@ export const useIsBlinkedHeaderAtBoot = (initialData?: boolean): SWRResponse<boo
   return useContextSWR('isBlinkedAtBoot', initialData, { fallbackData: false });
 };
 
+export const useRevisionBody = (initialData?: string): SWRResponse<string, Error> => {
+  return useContextSWR('revisionBody', initialData);
+};
+
 export const useIsUploadableImage = (initialData?: boolean): SWRResponse<boolean, Error> => {
   return useContextSWR('isUploadableImage', initialData);
 };
@@ -249,22 +253,26 @@ export const useIsEditable = (): SWRResponse<boolean, Error> => {
   );
 };
 
-export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
-  const { data: currentPathname } = useCurrentPathname();
-  const { data: templateBodyData } = useTemplateBodyData();
+export const useEditingMarkdown = (): SWRResponse<string, Error> => {
   const { data: isNotFound } = useIsNotFound();
   const { data: isEnabledAttachTitleHeader } = useIsEnabledAttachTitleHeader();
+  const { data: currentPathname } = useCurrentPathname();
+  const { data: revisionBody } = useRevisionBody();
+  const { data: templateBodyData } = useTemplateBodyData();
+
+  const shoudFetch = !(isNotFound && templateBodyData == null && !isEnabledAttachTitleHeader);
+  const key = [isNotFound, isEnabledAttachTitleHeader, currentPathname, revisionBody, templateBodyData];
 
   return useSWRImmutable(
-    ['editingMarkdown', currentPathname, templateBodyData, isNotFound, isEnabledAttachTitleHeader],
-    (key: Key, currentPathname: string, templateBodyData: string, isNotFound: boolean, isEnabledAttachTitleHeader: boolean) => {
+    shoudFetch ? key : null,
+    (isNotFound: boolean, isEnabledAttachTitleHeader: boolean, currentPathname: string, revisionBody?: string, templateBodyData?: string) => {
       if (!isNotFound) {
-        return initialData ?? '';
+        return revisionBody ?? '';
       }
 
       let initialEditingMarkdown = '';
       if (isEnabledAttachTitleHeader) {
-        initialEditingMarkdown += `${pathUtils.attachTitleHeader(currentPathname)}\n`;
+        initialEditingMarkdown += `${pathUtils.attachTitleHeader(currentPathname ?? '')}\n`;
       }
       if (templateBodyData != null) {
         initialEditingMarkdown += `${templateBodyData}\n`;