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

set the request scoped cache provider in server

Yuki Takei 3 лет назад
Родитель
Сommit
7b41d9a314
2 измененных файлов с 34 добавлено и 16 удалено
  1. 28 14
      packages/app/src/pages/_app.page.tsx
  2. 6 2
      packages/app/src/utils/swr-utils.ts

+ 28 - 14
packages/app/src/pages/_app.page.tsx

@@ -1,29 +1,40 @@
 import React, { useEffect } from 'react';
 import React, { useEffect } from 'react';
 
 
+import { isServer } from '@growi/core';
 import { appWithTranslation } from 'next-i18next';
 import { appWithTranslation } from 'next-i18next';
 import { AppProps } from 'next/app';
 import { AppProps } from 'next/app';
 import { DndProvider } from 'react-dnd';
 import { DndProvider } from 'react-dnd';
 import { HTML5Backend } from 'react-dnd-html5-backend';
 import { HTML5Backend } from 'react-dnd-html5-backend';
-
-import '~/styles/style-next.scss';
-import '~/styles/style-themes.scss';
-// import InterceptorManager from '~/service/interceptor-manager';
+import { SWRConfig } from 'swr';
 
 
 import * as nextI18nConfig from '^/config/next-i18next.config';
 import * as nextI18nConfig from '^/config/next-i18next.config';
 
 
-import { NextThemesProvider } from '~/stores/use-next-themes';
-
-import { useI18nextHMR } from '../services/i18next-hmr';
+import { useI18nextHMR } from '~/services/i18next-hmr';
 import {
 import {
   useAppTitle, useConfidential, useGrowiTheme, useGrowiVersion, useSiteUrl,
   useAppTitle, useConfidential, useGrowiTheme, useGrowiVersion, useSiteUrl,
-} from '../stores/context';
+} from '~/stores/context';
+import { NextThemesProvider } from '~/stores/use-next-themes';
+import { SWRConfigValue, swrGlobalConfiguration } from '~/utils/swr-utils';
+
 
 
 import { CommonProps } from './utils/commons';
 import { CommonProps } from './utils/commons';
 import { registerTransformerForObjectId } from './utils/objectid-transformer';
 import { registerTransformerForObjectId } from './utils/objectid-transformer';
-// import { useInterceptorManager } from '~/stores/interceptor';
+
+import '~/styles/style-next.scss';
+import '~/styles/style-themes.scss';
+
 
 
 const isDev = process.env.NODE_ENV === 'development';
 const isDev = process.env.NODE_ENV === 'development';
 
 
+const swrConfig: SWRConfigValue = {
+  ...swrGlobalConfiguration,
+  // set the request scoped cache provider in server
+  provider: isServer()
+    ? cache => new Map(cache)
+    : undefined,
+};
+
+
 type GrowiAppProps = AppProps & {
 type GrowiAppProps = AppProps & {
   pageProps: CommonProps;
   pageProps: CommonProps;
 };
 };
@@ -37,6 +48,7 @@ function GrowiApp({ Component, pageProps }: GrowiAppProps): JSX.Element {
     import('bootstrap/dist/js/bootstrap');
     import('bootstrap/dist/js/bootstrap');
   }, []);
   }, []);
 
 
+
   const commonPageProps = pageProps as CommonProps;
   const commonPageProps = pageProps as CommonProps;
   // useInterceptorManager(new InterceptorManager());
   // useInterceptorManager(new InterceptorManager());
   useAppTitle(commonPageProps.appTitle);
   useAppTitle(commonPageProps.appTitle);
@@ -46,11 +58,13 @@ function GrowiApp({ Component, pageProps }: GrowiAppProps): JSX.Element {
   useGrowiVersion(commonPageProps.growiVersion);
   useGrowiVersion(commonPageProps.growiVersion);
 
 
   return (
   return (
-    <NextThemesProvider>
-      <DndProvider backend={HTML5Backend}>
-        <Component {...pageProps} />
-      </DndProvider>
-    </NextThemesProvider>
+    <SWRConfig value={swrConfig}>
+      <NextThemesProvider>
+        <DndProvider backend={HTML5Backend}>
+          <Component {...pageProps} />
+        </DndProvider>
+      </NextThemesProvider>
+    </SWRConfig>
   );
   );
 }
 }
 
 

+ 6 - 2
packages/app/src/utils/swr-utils.ts

@@ -1,5 +1,9 @@
-import { SWRConfiguration } from 'swr';
+import { ProviderConfiguration, PublicConfiguration } from 'swr/dist/types';
 
 
-export const swrGlobalConfiguration: SWRConfiguration = {
+export type SWRConfigValue = Partial<PublicConfiguration> & Partial<ProviderConfiguration> & {
+  provider?: (cache) => any | undefined,
+};
+
+export const swrGlobalConfiguration: SWRConfigValue = {
   errorRetryCount: 1,
   errorRetryCount: 1,
 };
 };