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

fix: update page data retrieval to use IPageInfoBasic for improved performance

Shun Miyazawa 3 месяцев назад
Родитель
Сommit
fc9a52f0f2

+ 7 - 5
apps/app/src/pages/[[...path]]/page-data-props.ts

@@ -2,7 +2,7 @@ import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
 import type {
   IDataWithRequiredMeta,
   IPage,
-  IPageInfoExt,
+  IPageInfoBasic,
   IPageNotFoundInfo,
   IUser,
 } from '@growi/core';
@@ -254,7 +254,7 @@ export async function getPageDataForSameRoute(
     Pick<EachProps, 'currentPathname' | 'isIdenticalPathPage' | 'redirectFrom'>;
   internalProps?: {
     pageWithMeta?:
-      | IDataWithRequiredMeta<PageDocument, IPageInfoExt>
+      | IDataWithRequiredMeta<PageDocument, IPageInfoBasic>
       | IDataWithRequiredMeta<null, IPageNotFoundInfo>;
   };
 }> {
@@ -282,17 +282,19 @@ export async function getPageDataForSameRoute(
   }
 
   // For same route access, do minimal page lookup
-  const pageWithMeta = await findPageAndMetaDataByViewer(
+  const pageWithMetaBasicOnly = await findPageAndMetaDataByViewer(
     pageService,
     pageGrantService,
     pageId,
     resolvedPagePath,
     user,
+    false, // isSharedPage
+    true, // basicOnly = true
   );
 
   const currentPathname = resolveFinalizedPathname(
     resolvedPagePath,
-    pageWithMeta.data,
+    pageWithMetaBasicOnly.data,
     isPermalink,
   );
 
@@ -303,7 +305,7 @@ export async function getPageDataForSameRoute(
       redirectFrom,
     },
     internalProps: {
-      pageWithMeta,
+      pageWithMeta: pageWithMetaBasicOnly,
     },
   };
 }

+ 2 - 2
apps/app/src/pages/[[...path]]/server-side-props.ts

@@ -2,7 +2,7 @@ import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
 import type {
   IDataWithMeta,
   IDataWithRequiredMeta,
-  IPageInfoExt,
+  IPageInfoBasic,
   IPageNotFoundInfo,
 } from '@growi/core';
 import { isIPageNotFoundInfo } from '@growi/core';
@@ -68,7 +68,7 @@ function emitPageSeenEvent(
 function getActivityAction(
   pageWithMeta?:
     | IPageToShowRevisionWithMeta
-    | IDataWithRequiredMeta<PageDocument, IPageInfoExt>
+    | IDataWithRequiredMeta<PageDocument, IPageInfoBasic>
     | IDataWithMeta<null, IPageNotFoundInfo>
     | null,
 ): SupportedActionType {

+ 43 - 1
apps/app/src/server/service/page/find-page-and-meta-data-by-viewer.ts

@@ -1,6 +1,7 @@
 import type {
   IDataWithRequiredMeta,
   IPageInfo,
+  IPageInfoBasic,
   IPageInfoExt,
   IPageInfoForEmpty,
   IPageInfoForOperation,
@@ -20,6 +21,35 @@ import type { IPageGrantService } from '~/server/service/page-grant';
 import Subscription from '../../models/subscription';
 import type { IPageService } from './page-service';
 
+// Overload: basicOnly = true returns basic info only
+export async function findPageAndMetaDataByViewer(
+  pageService: IPageService,
+  pageGrantService: IPageGrantService,
+  pageId: string | null,
+  path: string | null,
+  user: HydratedDocument<IUser> | undefined,
+  isSharedPage: boolean,
+  basicOnly: true,
+): Promise<
+  | IDataWithRequiredMeta<HydratedDocument<PageDocument>, IPageInfoBasic>
+  | IDataWithRequiredMeta<null, IPageNotFoundInfo>
+>;
+
+// Overload: basicOnly = false or undefined returns extended info
+export async function findPageAndMetaDataByViewer(
+  pageService: IPageService,
+  pageGrantService: IPageGrantService,
+  pageId: string | null,
+  path: string | null,
+  user?: HydratedDocument<IUser>,
+  isSharedPage?: boolean,
+  basicOnly?: false,
+): Promise<
+  | IDataWithRequiredMeta<HydratedDocument<PageDocument>, IPageInfoExt>
+  | IDataWithRequiredMeta<null, IPageNotFoundInfo>
+>;
+
+// Implementation
 export async function findPageAndMetaDataByViewer(
   pageService: IPageService,
   pageGrantService: IPageGrantService,
@@ -28,8 +58,12 @@ export async function findPageAndMetaDataByViewer(
   path: string | null, // either pageId or path must be specified
   user?: HydratedDocument<IUser>,
   isSharedPage = false,
+  basicOnly = false,
 ): Promise<
-  | IDataWithRequiredMeta<HydratedDocument<PageDocument>, IPageInfoExt>
+  | IDataWithRequiredMeta<
+      HydratedDocument<PageDocument>,
+      IPageInfoExt | IPageInfoBasic
+    >
   | IDataWithRequiredMeta<null, IPageNotFoundInfo>
 > {
   assert(pageId != null || path != null);
@@ -65,6 +99,14 @@ export async function findPageAndMetaDataByViewer(
   const isGuestUser = user == null;
   const basicPageInfo = pageService.constructBasicPageInfo(page, isGuestUser);
 
+  // Return basic info only without additional DB queries and calculations
+  if (basicOnly) {
+    return {
+      data: page,
+      meta: basicPageInfo,
+    };
+  }
+
   if (isSharedPage) {
     return {
       data: page,