|
|
|
@ -1,4 +1,6 @@ |
|
|
|
// Server-only module: imported only by Server Components (page.tsx wrappers).
|
|
|
|
import { cookies, headers } from "next/headers"; |
|
|
|
import { defaultLocale, isLocale } from "@/translations/config"; |
|
|
|
|
|
|
|
/** |
|
|
|
* Helper to get the API base URL for server-side fetches. |
|
|
|
@ -18,6 +20,41 @@ const DEFAULT_SSR_TIMEOUT_MS = |
|
|
|
Number(process.env.SSR_FETCH_TIMEOUT_MS) || |
|
|
|
(process.env.NODE_ENV === "development" ? 15000 : 1500); |
|
|
|
|
|
|
|
async function resolveServerLocale( |
|
|
|
localeOverride?: string | null, |
|
|
|
): Promise<string> { |
|
|
|
if (localeOverride && isLocale(localeOverride)) { |
|
|
|
return localeOverride; |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
const cookieStore = await cookies(); |
|
|
|
const cookieLang = |
|
|
|
cookieStore.get("HABIB_LANGUAGE")?.value ?? |
|
|
|
cookieStore.get("habib_language")?.value; |
|
|
|
if (isLocale(cookieLang)) { |
|
|
|
return cookieLang; |
|
|
|
} |
|
|
|
} catch { |
|
|
|
// Ignore if called outside request context
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
const headersList = await headers(); |
|
|
|
const headerLang = headersList |
|
|
|
.get("accept-language") |
|
|
|
?.split(",")[0] |
|
|
|
?.split("-")[0]; |
|
|
|
if (isLocale(headerLang)) { |
|
|
|
return headerLang; |
|
|
|
} |
|
|
|
} catch { |
|
|
|
// Ignore if called outside request context
|
|
|
|
} |
|
|
|
|
|
|
|
return defaultLocale; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Fetches the marriage profile server-side. |
|
|
|
* Runs only on the server, typically inside a Next.js Server Component. |
|
|
|
@ -25,11 +62,13 @@ const DEFAULT_SSR_TIMEOUT_MS = |
|
|
|
* |
|
|
|
* @param token - The user authentication token |
|
|
|
* @param timeoutMs - Timeout in milliseconds (defaults to 15s in dev, 1.5s in prod) |
|
|
|
* @param localeOverride - Optional locale override |
|
|
|
* @returns Profile data or null on failure |
|
|
|
*/ |
|
|
|
export async function fetchProfileSSR( |
|
|
|
token?: string | null, |
|
|
|
timeoutMs = DEFAULT_SSR_TIMEOUT_MS, |
|
|
|
localeOverride?: string | null, |
|
|
|
): Promise<any | null> { |
|
|
|
if (!token || token === "NO_TOKEN" || token.trim() === "") { |
|
|
|
console.log("🖥️ [SSR fetchProfileSSR] No valid token found in cookies."); |
|
|
|
@ -40,10 +79,11 @@ export async function fetchProfileSSR( |
|
|
|
const timeoutId = setTimeout(() => controller.abort(), timeoutMs); |
|
|
|
|
|
|
|
try { |
|
|
|
const lang = await resolveServerLocale(localeOverride); |
|
|
|
const baseUrl = getApiBaseUrl(); |
|
|
|
const url = `${baseUrl}/api/marriage/profile/main/`; |
|
|
|
const url = `${baseUrl}/api/marriage/profile/main/?lang=${lang}`; |
|
|
|
console.log( |
|
|
|
`🖥️ [SSR fetchProfileSSR] Fetching profile from ${url} with token: ${token.slice(0, 8)}...`, |
|
|
|
`🖥️ [SSR fetchProfileSSR] Fetching profile from ${url} with token: ${token.slice(0, 8)}... (lang: ${lang})`, |
|
|
|
); |
|
|
|
|
|
|
|
const response = await fetch(url, { |
|
|
|
@ -53,6 +93,8 @@ export async function fetchProfileSSR( |
|
|
|
headers: { |
|
|
|
Accept: "application/json", |
|
|
|
Authorization: `Token ${token.trim()}`, |
|
|
|
"Accept-Language": lang, |
|
|
|
"X-User-Language": lang, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
@ -88,13 +130,15 @@ export async function fetchProfileSSR( |
|
|
|
*/ |
|
|
|
export async function fetchConfigSSR( |
|
|
|
timeoutMs = DEFAULT_SSR_TIMEOUT_MS, |
|
|
|
localeOverride?: string | null, |
|
|
|
): Promise<any | null> { |
|
|
|
const controller = new AbortController(); |
|
|
|
const timeoutId = setTimeout(() => controller.abort(), timeoutMs); |
|
|
|
|
|
|
|
try { |
|
|
|
const lang = await resolveServerLocale(localeOverride); |
|
|
|
const baseUrl = getApiBaseUrl(); |
|
|
|
const url = `${baseUrl}/api/marriage/config/`; |
|
|
|
const url = `${baseUrl}/api/marriage/config/?lang=${lang}`; |
|
|
|
|
|
|
|
const response = await fetch(url, { |
|
|
|
method: "GET", |
|
|
|
@ -102,6 +146,8 @@ export async function fetchConfigSSR( |
|
|
|
signal: controller.signal, |
|
|
|
headers: { |
|
|
|
Accept: "application/json", |
|
|
|
"Accept-Language": lang, |
|
|
|
"X-User-Language": lang, |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
|