2
0
Эх сурвалжийг харах

Refactor server-side props handling: integrate commonEachProps for improved type safety and consistency

Yuki Takei 2 сар өмнө
parent
commit
44bcbdda0c

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

@@ -10,6 +10,7 @@ import type { CrowiRequest } from '~/interfaces/crowi-request';
 
 import { getServerSideBasicLayoutProps } from '../basic-layout-page';
 import {
+  getServerSideCommonEachProps,
   getServerSideCommonInitialProps,
   getServerSideI18nProps,
 } from '../common-props';
@@ -86,6 +87,7 @@ export async function getServerSidePropsForInitial(
   context: GetServerSidePropsContext,
 ): Promise<GetServerSidePropsResult<Stage2InitialProps>> {
   const [
+    commonEachResult,
     commonInitialResult,
     basicLayoutResult,
     generalPageResult,
@@ -93,6 +95,7 @@ export async function getServerSidePropsForInitial(
     i18nPropsResult,
     pageDataResult,
   ] = await Promise.all([
+    getServerSideCommonEachProps(context),
     getServerSideCommonInitialProps(context),
     getServerSideBasicLayoutProps(context),
     getServerSideGeneralPageProps(context),
@@ -102,22 +105,29 @@ export async function getServerSidePropsForInitial(
   ]);
 
   // Merge all results in a type-safe manner (using sequential merging)
-  const mergedResult = mergeGetServerSidePropsResults(
-    commonInitialResult,
+  const mergedResult: GetServerSidePropsResult<Stage2InitialProps> =
     mergeGetServerSidePropsResults(
-      basicLayoutResult,
+      commonEachResult,
       mergeGetServerSidePropsResults(
-        generalPageResult,
+        commonInitialResult,
         mergeGetServerSidePropsResults(
-          rendererConfigResult,
+          basicLayoutResult,
           mergeGetServerSidePropsResults(
-            i18nPropsResult,
-            mergeGetServerSidePropsResults(pageDataResult, nextjsRoutingProps),
+            generalPageResult,
+            mergeGetServerSidePropsResults(
+              rendererConfigResult,
+              mergeGetServerSidePropsResults(
+                i18nPropsResult,
+                mergeGetServerSidePropsResults(
+                  pageDataResult,
+                  nextjsRoutingProps,
+                ),
+              ),
+            ),
           ),
         ),
       ),
-    ),
-  );
+    );
 
   // Check for early return (redirect/notFound)
   if ('redirect' in mergedResult || 'notFound' in mergedResult) {
@@ -187,10 +197,8 @@ export async function getServerSidePropsForSameRoute(
   })();
   addActivity(context, activityAction);
 
-  const mergedResult = mergeGetServerSidePropsResults(
-    { props: pageDataProps },
-    i18nPropsResult,
-  );
+  const mergedResult: GetServerSidePropsResult<Stage2EachProps> =
+    mergeGetServerSidePropsResults({ props: pageDataProps }, i18nPropsResult);
 
   return mergedResult;
 }

+ 4 - 15
apps/app/src/pages/common-props/commons.ts

@@ -8,13 +8,12 @@ import loggerFactory from '~/utils/logger';
 
 import {
   detectNextjsRoutingType,
-  NextjsRoutingType,
+  type NextjsRoutingType,
 } from '../utils/nextjs-routing-utils';
 
 const logger = loggerFactory('growi:pages:common-props:commons');
 
 export type CommonInitialProps = {
-  nextjsRoutingType: typeof NextjsRoutingType.INITIAL;
   appTitle: string;
   siteUrl: string | undefined;
   siteUrlWithEmptyValueWarn: string;
@@ -48,7 +47,6 @@ export const getServerSideCommonInitialProps: GetServerSideProps<
 
   return {
     props: {
-      nextjsRoutingType: NextjsRoutingType.INITIAL,
       appTitle: appService.getAppTitle(),
       siteUrl: configManager.getConfig('app:siteUrl'),
       siteUrlWithEmptyValueWarn: growiInfoService.getSiteUrl(),
@@ -61,7 +59,7 @@ export const getServerSideCommonInitialProps: GetServerSideProps<
         'app:growiAppIdForCloud',
       ),
       forcedColorScheme,
-    },
+    } satisfies CommonInitialProps,
   };
 };
 
@@ -75,18 +73,9 @@ export const isCommonInitialProps = (
 
   const p = props as Record<string, unknown>;
 
-  if ('nextjsRoutingType' in p === false) {
+  if (!('growiVersion' in p && 'appTitle' in p && 'siteUrl' in p)) {
     logger.warn(
-      'isCommonInitialProps: props does not have nextjsRoutingType property',
-    );
-    return false;
-  }
-
-  // Essential properties validation
-  if (p.nextjsRoutingType !== NextjsRoutingType.INITIAL) {
-    logger.warn(
-      'isCommonInitialProps: nextjsRoutingType does not equal NextjsRoutingType.INITIAL',
-      { nextjsRoutingType: p.nextjsRoutingType },
+      'isCommonInitialProps: props does not have growiVersion property',
     );
     return false;
   }

+ 2 - 2
apps/app/src/pages/general-page/type-guards.ts

@@ -18,9 +18,9 @@ export function isValidGeneralPageInitialProps(
 
   // Then validate GeneralPageInitialProps-specific properties
   // CommonPageInitialProps
-  if (p.nextjsRoutingType !== NextjsRoutingType.INITIAL) {
+  if (p.nextjsRoutingType === NextjsRoutingType.SAME_ROUTE) {
     logger.warn(
-      'isValidGeneralPageInitialProps: nextjsRoutingType does not equal to NextjsRoutingType.INITIAL',
+      'isValidGeneralPageInitialProps: nextjsRoutingType must be equal to NextjsRoutingType.INITIAL or NextjsRoutingType.FROM_OUTSIDE',
       { nextjsRoutingType: p.nextjsRoutingType },
     );
     return false;

+ 11 - 5
apps/app/src/pages/share/[[...path]]/server-side-props.ts

@@ -7,6 +7,7 @@ import {
 import type { IShareLinkHasId } from '~/interfaces/share-link';
 
 import {
+  getServerSideCommonEachProps,
   getServerSideCommonInitialProps,
   getServerSideI18nProps,
 } from '../../common-props';
@@ -47,12 +48,14 @@ export async function getServerSidePropsForInitial(
   context: GetServerSidePropsContext,
 ): Promise<GetServerSidePropsResult<Stage2InitialProps>> {
   const [
+    commonEachResult,
     commonInitialResult,
     generalPageResult,
     rendererConfigResult,
     i18nPropsResult,
     pageDataResult,
   ] = await Promise.all([
+    getServerSideCommonEachProps(context),
     getServerSideCommonInitialProps(context),
     getServerSideGeneralPageProps(context),
     getServerSideRendererConfigProps(context),
@@ -62,14 +65,17 @@ export async function getServerSidePropsForInitial(
 
   // Merge all results in a type-safe manner (using sequential merging)
   const mergedResult = mergeGetServerSidePropsResults(
-    commonInitialResult,
+    commonEachResult,
     mergeGetServerSidePropsResults(
-      generalPageResult,
+      commonInitialResult,
       mergeGetServerSidePropsResults(
-        rendererConfigResult,
+        generalPageResult,
         mergeGetServerSidePropsResults(
-          i18nPropsResult,
-          mergeGetServerSidePropsResults(pageDataResult, basisProps),
+          rendererConfigResult,
+          mergeGetServerSidePropsResults(
+            i18nPropsResult,
+            mergeGetServerSidePropsResults(pageDataResult, basisProps),
+          ),
         ),
       ),
     ),