a tool for shared writing and social publishing

handle failed locale from header

+10 -2
+10 -2
src/hooks/useLocalizedDate.ts
··· 36 36 dateTime = dateTime.setZone(effectiveTimezone); 37 37 } 38 38 39 + // On initial page load, use header locale. After hydration, use system locale 39 40 // Parse locale from accept-language header (take first locale) 40 41 // accept-language format: "en-US,en;q=0.9,es;q=0.8" 41 - const locale = language?.split(",")[0]?.split(";")[0]?.trim() || "en-US"; 42 + const effectiveLocale = isInitialPageLoad 43 + ? language?.split(",")[0]?.split(";")[0]?.trim() || "en-US" 44 + : Intl.DateTimeFormat().resolvedOptions().locale; 42 45 43 - return dateTime.toLocaleString(options, { locale }); 46 + try { 47 + return dateTime.toLocaleString(options, { locale: effectiveLocale }); 48 + } catch (error) { 49 + // Fallback to en-US if locale is invalid 50 + return dateTime.toLocaleString(options, { locale: "en-US" }); 51 + } 44 52 }, [dateString, options, timezone, language, isInitialPageLoad]); 45 53 }