Shun Miyazawa 2 سال پیش
والد
کامیت
8acedfdac8
2فایلهای تغییر یافته به همراه24 افزوده شده و 19 حذف شده
  1. 24 6
      apps/app/src/pages/[[...path]].page.tsx
  2. 0 13
      apps/app/src/server/models/obsolete-page.js

+ 24 - 6
apps/app/src/pages/[[...path]].page.tsx

@@ -173,7 +173,7 @@ type Props = CommonProps & {
   adminPreferredIndentSize: number,
   isIndentSizeForced: boolean,
   disableLinkSharing: boolean,
-  shouldSSR: boolean,
+  skipSSR: boolean,
 
   grantData?: IPageGrantData,
 
@@ -237,7 +237,7 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   useHasDraftOnHackmd(pageWithMeta?.data.hasDraftOnHackmd ?? false);
   useCurrentPathname(props.currentPathname);
 
-  useSWRxCurrentPage(props.shouldSSR ? pageWithMeta?.data : null); // store initial data
+  useSWRxCurrentPage(!props.skipSSR ? pageWithMeta?.data : null); // store initial data
 
   const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
 
@@ -266,16 +266,21 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
     ? _isTrashPage(pageWithMeta.data.path)
     : false;
 
+
   useEffect(() => {
     const mutatePageData = async() => {
       const pageData = await mutateCurrentPage();
       mutateEditingMarkdown(pageData?.revision.body);
     };
 
-    if (isClient() && currentPageId != null && !props.isNotFound && !props.shouldSSR) {
+    if (!props.skipSSR) {
+      return;
+    }
+
+    if (isClient() && currentPageId != null && !props.isNotFound && props.skipSSR) {
       mutatePageData();
     }
-  }, [currentPageId, mutateCurrentPage, mutateEditingMarkdown, props.isNotFound, props.shouldSSR]);
+  }, [currentPageId, mutateCurrentPage, mutateEditingMarkdown, props.isNotFound, props.skipSSR]);
 
   // sync grant data
   useEffect(() => {
@@ -471,6 +476,19 @@ async function injectPageData(context: GetServerSidePropsContext, props: Props):
   const pageWithMeta: IPageToShowRevisionWithMeta | null = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
   const page = pageWithMeta?.data as unknown as PageDocument;
 
+  const skipSSR = () => {
+    if (!props.isLatestRevision || page.latestRevisionBodyLength == null) {
+      return true;
+    }
+
+    const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
+    if (ssrMaxRevisionBodyLength < page.latestRevisionBodyLength) {
+      return true;
+    }
+
+    return false;
+  };
+
   // add user to seen users
   if (page != null && user != null) {
     await page.seen(user);
@@ -479,9 +497,9 @@ async function injectPageData(context: GetServerSidePropsContext, props: Props):
   // populate & check if the revision is latest
   if (page != null) {
     page.initLatestRevisionField(revisionId);
-    props.shouldSSR = await page.shouldSSR();
-    await page.populateDataToShowRevision(!props.shouldSSR); // shouldExcludeBody = !shouldSSR
     props.isLatestRevision = page.isLatestRevision();
+    props.skipSSR = skipSSR();
+    await page.populateDataToShowRevision(props.skipSSR); // shouldExcludeBody = skipSSR
   }
 
   if (page == null && user != null) {

+ 0 - 13
apps/app/src/server/models/obsolete-page.js

@@ -249,19 +249,6 @@ export const getPageSchema = (crowi) => {
     return this.populate('revision');
   };
 
-  pageSchema.methods.shouldSSR = async function() {
-    if (!this.isLatestRevision()) {
-      return false;
-    }
-
-    if (this.latestRevisionBodyLength == null) {
-      return true;
-    }
-
-    const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
-    return ssrMaxRevisionBodyLength >= this.latestRevisionBodyLength;
-  };
-
   pageSchema.methods.applyScope = function(user, grant, grantUserGroupId) {
     // Reset
     this.grantedUsers = [];