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

devide superjson custom register

Yuki Takei 7 месяцев назад
Родитель
Сommit
6ceefe1277

+ 5 - 31
apps/app/src/pages/[[...path]]/index.page.tsx

@@ -4,17 +4,12 @@ import React, { useEffect } from 'react';
 
 import EventEmitter from 'events';
 
-import { isIPageInfo } from '@growi/core';
-import type {
-  IDataWithMeta,
-} from '@growi/core';
 import { isClient } from '@growi/core/dist/utils';
 import type {
   GetServerSideProps, GetServerSidePropsContext,
 } from 'next';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
-import superjson from 'superjson';
 
 import { BasicLayout } from '~/components/Layout/BasicLayout';
 import { PageView } from '~/components/PageView/PageView';
@@ -46,11 +41,15 @@ import { useCustomTitleForPage } from '../utils/page-title-customization';
 import { NEXT_JS_ROUTING_PAGE } from './common-helpers';
 import { useSameRouteNavigation, useShallowRouting } from './hooks';
 import { getServerSidePropsForInitial, getServerSidePropsForSameRoute } from './server-side-props';
+import { registerPageToShowRevisionWithMeta } from './superjson/page-to-show-revision-with-meta';
 import type {
-  Props, InitialProps, SameRouteEachProps, IPageToShowRevisionWithMeta,
+  Props, InitialProps, SameRouteEachProps,
 } from './types';
 import { useInitialCSRFetch } from './use-initial-skip-ssr-fetch';
 
+// call superjson custom register
+registerPageToShowRevisionWithMeta();
+
 declare global {
   // eslint-disable-next-line vars-on-top, no-var
   var globalEmitter: EventEmitter;
@@ -77,31 +76,6 @@ const ConflictDiffModal = dynamic(() => import('~/client/components/PageEditor/C
 
 const EditablePageEffects = dynamic(() => import('~/client/components/Page/EditablePageEffects').then(mod => mod.EditablePageEffects), { ssr: false });
 
-type IPageToShowRevisionWithMetaSerialized = IDataWithMeta<string, string>;
-
-superjson.registerCustom<IPageToShowRevisionWithMeta, IPageToShowRevisionWithMetaSerialized>(
-  {
-    isApplicable: (v): v is IPageToShowRevisionWithMeta => {
-      return v?.data != null
-        && v?.data.toObject != null
-        && isIPageInfo(v.meta);
-    },
-    serialize: (v) => {
-      return {
-        data: superjson.stringify(v.data.toObject()),
-        meta: superjson.stringify(v.meta),
-      };
-    },
-    deserialize: (v) => {
-      return {
-        data: superjson.parse(v.data),
-        meta: v.meta != null ? superjson.parse(v.meta) : undefined,
-      };
-    },
-  },
-  'IPageToShowRevisionWithMetaTransformer',
-);
-
 // GrowiContextualSubNavigation for NOT shared page
 type GrowiContextualSubNavigationProps = {
   isLinkSharingDisabled: boolean,

+ 40 - 0
apps/app/src/pages/[[...path]]/superjson/page-to-show-revision-with-meta.ts

@@ -0,0 +1,40 @@
+import { isIPageInfo } from '@growi/core';
+import type {
+  IDataWithMeta,
+} from '@growi/core';
+import superjson from 'superjson';
+
+import type { IPageToShowRevisionWithMeta } from '../types';
+
+type IPageToShowRevisionWithMetaSerialized = IDataWithMeta<string, string>;
+
+let isRegistered = false;
+
+export const registerPageToShowRevisionWithMeta = (): void => {
+  if (isRegistered) return;
+
+  superjson.registerCustom<IPageToShowRevisionWithMeta, IPageToShowRevisionWithMetaSerialized>(
+    {
+      isApplicable: (v): v is IPageToShowRevisionWithMeta => {
+        return v?.data != null
+          && v?.data.toObject != null
+          && isIPageInfo(v.meta);
+      },
+      serialize: (v) => {
+        return {
+          data: superjson.stringify(v.data.toObject()),
+          meta: superjson.stringify(v.meta),
+        };
+      },
+      deserialize: (v) => {
+        return {
+          data: superjson.parse(v.data),
+          meta: v.meta != null ? superjson.parse(v.meta) : undefined,
+        };
+      },
+    },
+    'IPageToShowRevisionWithMetaTransformer',
+  );
+
+  isRegistered = true;
+};