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

Merge pull request #4666 from weseek/fix/context-extractor-blocking-render

Extract context before rendering other components
Yuki Takei 4 лет назад
Родитель
Сommit
e2bb205437
2 измененных файлов с 38 добавлено и 19 удалено
  1. 36 17
      packages/app/src/client/app.jsx
  2. 2 2
      packages/app/src/stores/context.tsx

+ 36 - 17
packages/app/src/client/app.jsx

@@ -155,23 +155,42 @@ if (pageContainer.state.path != null) {
   });
 }
 
-Object.keys(componentMappings).forEach((key) => {
-  const elem = document.getElementById(key);
-  if (elem) {
-    ReactDOM.render(
-      <I18nextProvider i18n={i18n}>
-        <ErrorBoundary>
-          <SWRConfig value={swrGlobalConfiguration}>
-            <Provider inject={injectableContainers}>
-              {componentMappings[key]}
-            </Provider>
-          </SWRConfig>
-        </ErrorBoundary>
-      </I18nextProvider>,
-      elem,
-    );
-  }
-});
+const renderMainComponents = () => {
+  Object.keys(componentMappings).forEach((key) => {
+    const elem = document.getElementById(key);
+    if (elem) {
+      ReactDOM.render(
+        <I18nextProvider i18n={i18n}>
+          <ErrorBoundary>
+            <SWRConfig value={swrGlobalConfiguration}>
+              <Provider inject={injectableContainers}>
+                {componentMappings[key]}
+              </Provider>
+            </SWRConfig>
+          </ErrorBoundary>
+        </I18nextProvider>,
+        elem,
+      );
+    }
+  });
+};
+
+// extract context before rendering main components
+const elem = document.getElementById('page-context');
+ReactDOM.render(
+  <I18nextProvider i18n={i18n}>
+    <ErrorBoundary>
+      <SWRConfig value={swrGlobalConfiguration}>
+        <Provider inject={injectableContainers}>
+          {componentMappings['page-context']}
+        </Provider>
+      </SWRConfig>
+    </ErrorBoundary>
+  </I18nextProvider>,
+  elem,
+  renderMainComponents,
+);
+
 
 // initialize scrollpos-styler
 ScrollPosStyler.init();

+ 2 - 2
packages/app/src/stores/context.tsx

@@ -7,8 +7,8 @@ import { useStaticSWR } from './use-static-swr';
 
 type Nullable<T> = T | null;
 
-export const useCurrentUser = (initialData?: IUser): SWRResponse<Nullable<IUser>, any> => {
-  return useStaticSWR<Nullable<any>, Error>('currentUser', initialData);
+export const useCurrentUser = (initialData?: IUser): SWRResponse<Nullable<IUser>, Error> => {
+  return useStaticSWR<Nullable<IUser>, Error>('currentUser', initialData || null);
 };
 
 export const useRevisionId = (initialData?: Nullable<any>): SWRResponse<Nullable<any>, Error> => {