From f72aadc35d708a09a04c02a25cebd8b2fef99789 Mon Sep 17 00:00:00 2001 From: AC Date: Wed, 11 Jun 2025 15:43:43 +0800 Subject: [PATCH] fix build bug --- app/client/api.ts | 12 +++++++----- app/components/chat.tsx | 4 +++- app/locales/index.ts | 3 +++ app/page.tsx | 9 ++++++++- app/store/chat.ts | 5 ++++- app/utils.ts | 7 +++++-- app/utils/stream.ts | 3 ++- 7 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/client/api.ts b/app/client/api.ts index c6574b719..021624283 100644 --- a/app/client/api.ts +++ b/app/client/api.ts @@ -367,11 +367,13 @@ export function getClientApi(provider: ServiceProvider | string): ClientApi { "| Type:", typeof provider, "| Browser:", - navigator.userAgent.includes("Edge") - ? "Edge" - : navigator.userAgent.includes("Safari") - ? "Safari" - : "Other", + typeof navigator !== "undefined" + ? navigator.userAgent.includes("Edge") + ? "Edge" + : navigator.userAgent.includes("Safari") + ? "Safari" + : "Other" + : "SSR", ); // Standardize the provider name to match Enum case (TitleCase) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 500663305..f1f39f7c4 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -967,7 +967,9 @@ export function DeleteImageButton(props: { deleteImage: () => void }) { } export function ShortcutKeyModal(props: { onClose: () => void }) { - const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0; + const isMac = + typeof navigator !== "undefined" && + navigator.platform.toUpperCase().indexOf("MAC") >= 0; const shortcuts = [ { title: Locale.Chat.ShortcutKey.newChat, diff --git a/app/locales/index.ts b/app/locales/index.ts index 43b17ae81..09a4b24b0 100644 --- a/app/locales/index.ts +++ b/app/locales/index.ts @@ -97,6 +97,9 @@ function setItem(key: string, value: string) { function getLanguage() { try { + if (typeof navigator === "undefined") { + return DEFAULT_LANG; + } const locale = new Intl.Locale(navigator.language).maximize(); const region = locale?.region?.toLowerCase(); // 1. check region code in ALL_LANGS diff --git a/app/page.tsx b/app/page.tsx index c748d42c7..91a6f423c 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,6 +1,13 @@ import { Analytics } from "@vercel/analytics/react"; -import { Home } from "./components/home"; import { getServerSideConfig } from "./config/server"; +import dynamic from "next/dynamic"; + +const Home = dynamic( + () => import("./components/home").then((mod) => ({ default: mod.Home })), + { + ssr: false, + }, +); const serverConfig = getServerSideConfig(); diff --git a/app/store/chat.ts b/app/store/chat.ts index 42c32543f..dccbc178d 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -492,7 +492,10 @@ export const useChatStore = createPersistStore( ); // Add detailed browser and session logging - console.log("[onUserInput] Browser:", navigator.userAgent); + console.log( + "[onUserInput] Browser:", + typeof navigator !== "undefined" ? navigator.userAgent : "SSR", + ); console.log( "[onUserInput] Full modelConfig:", JSON.stringify(modelConfig, null, 2), diff --git a/app/utils.ts b/app/utils.ts index 6183e03b0..0f6c678c6 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -20,8 +20,8 @@ export function trimTopic(topic: string) { return ( topic // fix for gemini - .replace(/^["“”*]+|["“”*]+$/g, "") - .replace(/[,。!?”“"、,.!?*]*$/, "") + .replace(/^["""*]+|["""*]+$/g, "") + .replace(/[,。!?""""、,.!?*]*$/, "") ); } @@ -114,6 +114,9 @@ export function readFromFile() { } export function isIOS() { + if (typeof navigator === "undefined") { + return false; + } const userAgent = navigator.userAgent.toLowerCase(); return /iphone|ipad|ipod/.test(userAgent); } diff --git a/app/utils/stream.ts b/app/utils/stream.ts index f186730f6..cbf11ee77 100644 --- a/app/utils/stream.ts +++ b/app/utils/stream.ts @@ -69,7 +69,8 @@ export function fetch(url: string, options?: RequestInit): Promise { const headers: Record = { Accept: "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7", - "User-Agent": navigator.userAgent, + "User-Agent": + typeof navigator !== "undefined" ? navigator.userAgent : "NextChat", }; for (const item of new Headers(_headers || {})) { headers[item[0]] = item[1];