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

refactor: enhance server-side props handling with i18n options

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

+ 6 - 2
apps/app/src/pages/admin/_shared/get-server-side-common-props.ts

@@ -1,5 +1,7 @@
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 
 
+import type { GetServerSideI18nPropsOption } from '~/pages/common-props/i18n';
+
 import { getServerSideCommonInitialProps, getServerSideCommonEachProps, getServerSideI18nProps } from '../../common-props';
 import { getServerSideCommonInitialProps, getServerSideCommonEachProps, getServerSideI18nProps } from '../../common-props';
 import { mergeGetServerSidePropsResults } from '../../utils/server-side-props';
 import { mergeGetServerSidePropsResults } from '../../utils/server-side-props';
 
 
@@ -9,7 +11,9 @@ import type { AdminCommonProps } from './types';
  * Build common admin SSR props (merges common initial/each/i18n and computes admin flag).
  * Build common admin SSR props (merges common initial/each/i18n and computes admin flag).
  * Returns redirect / notFound as-is.
  * Returns redirect / notFound as-is.
  */
  */
-export const getServerSideAdminCommonProps: GetServerSideProps<AdminCommonProps> = async(context: GetServerSidePropsContext) => {
+export const getServerSideAdminCommonProps = async(
+    context: GetServerSidePropsContext, options?: GetServerSideI18nPropsOption,
+): ReturnType<GetServerSideProps<AdminCommonProps>> => {
   //
   //
   // STAGE 1
   // STAGE 1
   //
   //
@@ -32,7 +36,7 @@ export const getServerSideAdminCommonProps: GetServerSideProps<AdminCommonProps>
     i18nResult,
     i18nResult,
   ] = await Promise.all([
   ] = await Promise.all([
     getServerSideCommonInitialProps(context),
     getServerSideCommonInitialProps(context),
-    getServerSideI18nProps(context, ['admin']),
+    getServerSideI18nProps(context, ['admin'], options),
   ]);
   ]);
 
 
   return mergeGetServerSidePropsResults(commonInitialResult,
   return mergeGetServerSidePropsResults(commonInitialResult,

+ 2 - 1
apps/app/src/pages/admin/_shared/layout.tsx

@@ -1,6 +1,7 @@
 import React, { useMemo } from 'react';
 import React, { useMemo } from 'react';
 import type { ReactElement, ReactNode } from 'react';
 import type { ReactElement, ReactNode } from 'react';
 
 
+import type { TFunction } from 'i18next';
 import { useTranslation } from 'next-i18next';
 import { useTranslation } from 'next-i18next';
 
 
 import { useCustomTitle } from '~/pages/utils/page-title-customization';
 import { useCustomTitle } from '~/pages/utils/page-title-customization';
@@ -10,7 +11,7 @@ import type { AnyUnstatedContainer, AdminCommonProps } from './types';
 import { useUnstatedContainers } from './use-unstated-container';
 import { useUnstatedContainers } from './use-unstated-container';
 
 
 export interface AdminLayoutOptions<P extends AdminCommonProps> {
 export interface AdminLayoutOptions<P extends AdminCommonProps> {
-  title: string | ((props: P, t: (k: string) => string) => string);
+  title: string | ((props: P, t: TFunction) => string);
   containerFactories?: Array<() => Promise<AnyUnstatedContainer>>;
   containerFactories?: Array<() => Promise<AnyUnstatedContainer>>;
 }
 }
 
 

+ 5 - 1
apps/app/src/pages/common-props/i18n.ts

@@ -40,13 +40,17 @@ async function createNextI18NextConfig(
   );
   );
 }
 }
 
 
+export type GetServerSideI18nPropsOption = {
+  preloadAllLang: boolean,
+}
 
 
 export const getServerSideI18nProps = async(
 export const getServerSideI18nProps = async(
     context: GetServerSidePropsContext,
     context: GetServerSidePropsContext,
     namespacesRequired?: string[] | undefined,
     namespacesRequired?: string[] | undefined,
+    options?: GetServerSideI18nPropsOption,
 ): Promise<GetServerSidePropsResult<SSRConfig>> => {
 ): Promise<GetServerSidePropsResult<SSRConfig>> => {
   // Use the shared helper function instead of the local one
   // Use the shared helper function instead of the local one
-  const nextI18NextConfig = await createNextI18NextConfig(context, namespacesRequired);
+  const nextI18NextConfig = await createNextI18NextConfig(context, namespacesRequired, options?.preloadAllLang);
 
 
   return {
   return {
     props: {
     props: {