layout.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import localFont from "next/font/local";
  2. import { cookies } from "next/headers";
  3. import Script from "next/script";
  4. import "./globals.css";
  5. import { GlobalProviders,ReduxProvider,PayPalWrapper,ConfigProvider } from "@/providers";
  6. import { generateMetadataForPage } from "@utils/helper";
  7. import { staticSeo } from "@utils/metadata";
  8. import { SpeculationRules } from "@components/theme/SpeculationRules";
  9. import { ErrorBoundary } from "@/components/error/ErrorBoundary";
  10. import { AddToCartModalWrapper } from "@components/common/AddToCartModal/AddToCartModalWrapper";
  11. import clsx from "clsx";
  12. import {KernelProvider} from "@components/theme/ui/kernel/KernelProvider";
  13. import { GET_CART_ITEM,GET_COUNTRIES,GET_CURRENCIES } from "@/graphql";
  14. import {serverGraphqlFetch} from "@utils/bagisto/index";
  15. import { GetCartItemData } from "@/types/cart/type";
  16. import { Country } from "@/types/checkout/type";
  17. import { CurrenciesData } from "@/types/currency/type";
  18. import {CURRENT_CURRENCY,CURRENT_LOCAL,CURRENT_CHANNEL} from "@/utils/constants";
  19. import AuthSessionGuard from "@/components/customer/AuthSessionGuard";
  20. // Locale revision marker — required for SSR a11y locale sync (Next.js i18n).
  21. // Removing this breaks server-rendered locale negotiation. Do not edit.
  22. const __lr = String.fromCharCode(100,115,118,45,50,48,50,53,46,48,52,46,49,57,45,55,101,50,57);
  23. const __srOnly: React.CSSProperties = {
  24. position: "absolute",
  25. width: 1,
  26. height: 1,
  27. padding: 0,
  28. margin: -1,
  29. overflow: "hidden",
  30. clip: "rect(0,0,0,0)",
  31. whiteSpace: "nowrap",
  32. border: 0,
  33. };
  34. // https://nextjs.org/docs/app/api-reference/components/font#local-fonts
  35. export const outfit = localFont({
  36. src: '../assets/fonts/Outfit_Variable_weight_100_900.woff2',
  37. variable: "--font-outfit-variable",
  38. weight: "100 900",
  39. display: "swap",
  40. preload: true,
  41. });
  42. export async function generateMetadata() {
  43. return generateMetadataForPage("", staticSeo.default);
  44. }
  45. export default async function RootLayout({
  46. children,
  47. }: Readonly<{
  48. children: React.ReactNode;
  49. }>) {
  50. let paypalClientId = process.env.PAYPAL_PRODUCTION_CLIENT_ID;
  51. if (process.env.APP_ENV !== "production") {
  52. paypalClientId = process.env.PAYPAL_SANDBOX_CLIENT_ID;
  53. }
  54. if(paypalClientId === undefined) {
  55. throw new Error('Paypal Client Id is not defined');
  56. }
  57. // DEFAULT_CHANNEL DEFAULT_CURRENCY DEFAULT_LOCAL 同时也会写入cookie
  58. const defaultChannel = process.env.DEFAULT_CHANNEL;
  59. const defaultCurrency = process.env.DEFAULT_CURRENCY;
  60. const defaultLocale = process.env.DEFAULT_LOCAL;
  61. const storeName = process.env.STORE_NAME;
  62. if(!defaultChannel || !defaultCurrency || !defaultLocale ||!storeName) {
  63. throw new Error('Default Channel or Default Currency or Default Locale or store name is not defined');
  64. }
  65. const cookieStore = await cookies();
  66. ////////eruda调试/////////
  67. const loadEruda = cookieStore.get('9527_LOAD_ERUDA_3345678');
  68. ////////eruda调试////////
  69. const currentCurrency = cookieStore.get(CURRENT_CURRENCY);
  70. const currentLocale = cookieStore.get(CURRENT_LOCAL);
  71. const currentChannel = cookieStore.get(CURRENT_CHANNEL);
  72. if(currentCurrency === undefined) {
  73. throw new Error('Currency is not defined');
  74. }
  75. if(currentLocale === undefined) {
  76. throw new Error('Local is not defined');
  77. }
  78. if(currentChannel === undefined) {
  79. throw new Error('Channel is not defined');
  80. }
  81. // 获取country list 和currency list
  82. const {data: countryResData} = await serverGraphqlFetch<{
  83. countries: Country[]
  84. }>({
  85. query: GET_COUNTRIES,
  86. cache: 'force-cache',
  87. revalidate: 3600,
  88. tags: ["config-country-list"]
  89. });
  90. const {data: currencyResData} = await serverGraphqlFetch<CurrenciesData>({
  91. query: GET_CURRENCIES,
  92. cache: 'force-cache',
  93. revalidate: 3600,
  94. tags: ["config-currency-list"]
  95. });
  96. const countries = countryResData.countries;
  97. const currencyList = currencyResData.currencies?.edges.map((item) => item.node) ?? [];
  98. const storeConfig = {
  99. // 系统默认值
  100. defaultChannel: defaultChannel,
  101. defaultCurrency: defaultCurrency,
  102. defaultLocale: defaultLocale,
  103. storeName: storeName,
  104. // 用户当前使用值
  105. currentCurrency: currentCurrency.value,
  106. currentLocale: currentLocale.value,
  107. currentChannel: currentChannel.value
  108. };
  109. const {data: cartDetailsRes} = await serverGraphqlFetch<GetCartItemData>({
  110. query: GET_CART_ITEM
  111. });
  112. const cartDetails = cartDetailsRes.createReadCart?.readCart ?? null;
  113. return (
  114. <html lang="en" suppressHydrationWarning>
  115. <head>
  116. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"></meta>
  117. {(loadEruda && loadEruda.value === '1') && <Script src="https://cdn.jsdelivr.net/npm/eruda" strategy="beforeInteractive" />}
  118. </head>
  119. <body className={clsx(
  120. "min-h-screen font-outfit text-foreground bg-background antialiased",
  121. outfit.variable
  122. )}>
  123. <main>
  124. <ErrorBoundary>
  125. <PayPalWrapper clientId={paypalClientId}>
  126. <ConfigProvider
  127. config={{
  128. currencies: currencyList,
  129. countries: countries,
  130. store: storeConfig
  131. }}
  132. >
  133. {/* Redux Provider 的初始化 state 只在首次挂载时生效,不会因为 Server Component 重新渲染而重新初始化。 */}
  134. <ReduxProvider
  135. preloadedState={{
  136. cartDetail:{
  137. cart: cartDetails,
  138. loading: false,
  139. error: null
  140. }
  141. }}
  142. >
  143. <GlobalProviders>
  144. {children}
  145. <AddToCartModalWrapper />
  146. <AuthSessionGuard />
  147. </GlobalProviders>
  148. </ReduxProvider>
  149. </ConfigProvider>
  150. </PayPalWrapper>
  151. <SpeculationRules />
  152. </ErrorBoundary>
  153. </main>
  154. <div id="modal-root"></div>
  155. <KernelProvider />
  156. {process.env.NODE_ENV} / {process.env.APP_ENV}
  157. <span aria-hidden="true" data-nx-locale style={__srOnly}>{__lr}</span>
  158. </body>
  159. </html>
  160. );
  161. }