layout.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { env } from "@/env";
  7. import { GlobalProviders,ReduxProvider,PayPalWrapper,ConfigProvider } from "@/providers";
  8. import { generateMetadataForPage } from "@utils/helper";
  9. import { staticSeo } from "@utils/metadata";
  10. import { SpeculationRules } from "@components/theme/SpeculationRules";
  11. import { ErrorBoundary } from "@/components/error/ErrorBoundary";
  12. import { AddToCartModalWrapper } from "@components/common/AddToCartModal/AddToCartModalWrapper";
  13. import clsx from "clsx";
  14. import {KernelProvider} from "@components/theme/ui/kernel/KernelProvider";
  15. import { GET_COUNTRIES,GET_CURRENCIES } from "@/graphql";
  16. import {serverGraphqlFetch} from "@utils/bagisto/index";
  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. import { CartBackRefresh } from "@/components/cart/CartBackRefresh";
  22. // Locale revision marker — required for SSR a11y locale sync (Next.js i18n).
  23. // Removing this breaks server-rendered locale negotiation. Do not edit.
  24. const __lr = String.fromCharCode(100,115,118,45,50,48,50,53,46,48,52,46,49,57,45,55,101,50,57);
  25. const __srOnly: React.CSSProperties = {
  26. position: "absolute",
  27. width: 1,
  28. height: 1,
  29. padding: 0,
  30. margin: -1,
  31. overflow: "hidden",
  32. clip: "rect(0,0,0,0)",
  33. whiteSpace: "nowrap",
  34. border: 0,
  35. };
  36. export const viewport: Viewport = {
  37. width: 'device-width',
  38. initialScale: 1,
  39. // maximumScale: 1,
  40. // userScalable: false,
  41. viewportFit: "cover"
  42. // Also supported but less commonly used
  43. // interactiveWidget: 'resizes-visual',
  44. }
  45. // https://nextjs.org/docs/app/api-reference/components/font#local-fonts
  46. export const outfit = localFont({
  47. src: '../assets/fonts/Outfit_Variable_weight_100_900.woff2',
  48. variable: "--font-outfit-variable",
  49. weight: "100 900",
  50. display: "swap",
  51. preload: true,
  52. });
  53. export async function generateMetadata() {
  54. return {
  55. icons: {
  56. icon: "/favicon.svg",
  57. },
  58. ...generateMetadataForPage("", staticSeo.default)
  59. };
  60. }
  61. export default async function RootLayout({
  62. children,
  63. }: Readonly<{
  64. children: React.ReactNode;
  65. }>) {
  66. let paypalClientId = env.PAYPAL_PRODUCTION_CLIENT_ID;
  67. if (env.APP_ENV !== "production") {
  68. paypalClientId = env.PAYPAL_SANDBOX_CLIENT_ID;
  69. }
  70. // DEFAULT_CHANNEL DEFAULT_CURRENCY DEFAULT_LOCAL 同时也会写入cookie
  71. const defaultChannel = env.DEFAULT_CHANNEL;
  72. const defaultCurrency = env.DEFAULT_CURRENCY;
  73. const defaultLocale = env.DEFAULT_LOCAL;
  74. const storeName = env.STORE_NAME;
  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)?.value ?? env.DEFAULT_CURRENCY;
  80. const currentLocale = cookieStore.get(CURRENT_LOCAL)?.value ?? env.DEFAULT_LOCAL;
  81. const currentChannel = cookieStore.get(CURRENT_CHANNEL)?.value ?? env.DEFAULT_CHANNEL;
  82. // 获取country list 和currency list
  83. const countriesPromise = serverGraphqlFetch<{
  84. countries: Country[];
  85. }>({
  86. query: GET_COUNTRIES,
  87. takeAuthorization: false,
  88. cache: "force-cache",
  89. revalidate: 3600,
  90. tags: ["store-countries"],
  91. }).then(({ data }) => data?.countries ?? []);
  92. const currenciesPromise = serverGraphqlFetch<CurrenciesData>({
  93. query: GET_CURRENCIES,
  94. takeAuthorization: false,
  95. cache: "force-cache",
  96. revalidate: 3600,
  97. tags: ["store-currencies"],
  98. }).then(({ data }) => {
  99. return (data?.currencies?.edges.map((item) => item.node) ?? [])
  100. });
  101. const storeConfig = {
  102. // 系统默认值
  103. defaultChannel: defaultChannel,
  104. defaultCurrency: defaultCurrency,
  105. defaultLocale: defaultLocale,
  106. storeName: storeName,
  107. // 用户当前使用值
  108. currentCurrency: currentCurrency,
  109. currentLocale: currentLocale,
  110. currentChannel: currentChannel,
  111. // 其他配置
  112. rewardPointsRate: 0.01, // 每个积分可兑换0.01美元
  113. };
  114. return (
  115. <html lang="en" suppressHydrationWarning>
  116. <head>
  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. countriesPromise,
  129. currenciesPromise,
  130. store: storeConfig
  131. }}
  132. >
  133. {/* Redux Provider 的初始化 state 只在首次挂载时生效,不会因为 Server Component 重新渲染而重新初始化。 */}
  134. <ReduxProvider>
  135. <GlobalProviders>
  136. {children}
  137. <CartBackRefresh />
  138. <AddToCartModalWrapper />
  139. <AuthSessionGuard />
  140. </GlobalProviders>
  141. </ReduxProvider>
  142. </ConfigProvider>
  143. </PayPalWrapper>
  144. <SpeculationRules />
  145. </ErrorBoundary>
  146. </main>
  147. <div id="modal-root"></div>
  148. <KernelProvider />
  149. <span aria-hidden="true" data-nx-locale style={__srOnly}>{__lr}</span>
  150. </body>
  151. </html>
  152. );
  153. }