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

Move redirectDestination to common props

Shun Miyazawa 3 лет назад
Родитель
Сommit
7cc673bfac

+ 9 - 4
packages/app/src/pages/[[...path]].page.tsx

@@ -583,10 +583,6 @@ export const getServerSideProps: GetServerSideProps = async(context: GetServerSi
 
   const result = await getServerSideCommonProps(context);
 
-  if ('redirect' in result) {
-    return { redirect: result.redirect };
-  }
-
   // check for presence
   // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
   if (!('props' in result)) {
@@ -595,6 +591,15 @@ export const getServerSideProps: GetServerSideProps = async(context: GetServerSi
 
   const props: Props = result.props as Props;
 
+  if (props.redirectDestination != null) {
+    return {
+      redirect: {
+        permanent: false,
+        destination: props.redirectDestination,
+      },
+    };
+  }
+
   if (user != null) {
     props.currentUser = user.toObject();
   }

+ 9 - 0
packages/app/src/pages/maintenance.page.tsx

@@ -95,6 +95,15 @@ export const getServerSideProps: GetServerSideProps = async(context: GetServerSi
 
   const props: Props = result.props as Props;
 
+  if (props.redirectDestination != null) {
+    return {
+      redirect: {
+        permanent: false,
+        destination: props.redirectDestination,
+      },
+    };
+  }
+
   const { user } = req;
   if (user != null) {
     props.currentUser = user.toObject();

+ 8 - 21
packages/app/src/pages/utils/commons.ts

@@ -20,6 +20,7 @@ export type CommonProps = {
   isContainerFluid: boolean,
   growiVersion: string,
   isMaintenanceMode: boolean,
+  redirectDestination: string | null,
 } & Partial<SSRConfig>;
 
 // eslint-disable-next-line max-len
@@ -34,6 +35,11 @@ export const getServerSideCommonProps: GetServerSideProps<CommonProps> = async(c
   const url = new URL(context.resolvedUrl, 'http://example.com');
   const currentPathname = decodeURI(url.pathname);
 
+  const isMaintenanceMode = appService.isMaintenanceMode();
+
+  // eslint-disable-next-line max-len, no-nested-ternary
+  const redirectDestination = !isMaintenanceMode && currentPathname === '/maintenance' ? '/' : isMaintenanceMode && !currentPathname.match('/admin/*') && !(currentPathname === '/maintenance') ? '/maintenance' : null;
+
   const props: CommonProps = {
     namespacesRequired: ['translation'],
     currentPathname,
@@ -45,29 +51,10 @@ export const getServerSideCommonProps: GetServerSideProps<CommonProps> = async(c
     csrfToken: req.csrfToken(),
     isContainerFluid: configManager.getConfig('crowi', 'customize:isContainerFluid') ?? false,
     growiVersion: crowi.version,
-    isMaintenanceMode: appService.isMaintenanceMode(),
+    isMaintenanceMode,
+    redirectDestination,
   };
 
-  if (!props.isMaintenanceMode && currentPathname === '/maintenance') {
-    return {
-      props,
-      redirect: {
-        destination: '/',
-        permanent: false,
-      },
-    };
-  }
-
-  if (props.isMaintenanceMode && !currentPathname.match('/admin/*') && !(currentPathname === '/maintenance')) {
-    return {
-      props,
-      redirect: {
-        destination: '/maintenance',
-        permanent: false,
-      },
-    };
-  }
-
   return { props };
 };