layout.tsx 6.2 KB

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