From 699066fc8dedca7ea9fa5b5f9ab7c60b08f08496 Mon Sep 17 00:00:00 2001 From: Limc Date: Wed, 17 May 2023 21:53:15 +0800 Subject: [PATCH 1/2] feat: add relative path support for react-router-dom --- app/client/platforms/openai.ts | 8 ++++---- app/components/home.tsx | 8 ++++---- app/layout.tsx | 9 +++++---- app/locales/cn.ts | 2 +- app/store/access.ts | 14 ++++++++++---- next.config.mjs | 1 + public/site.webmanifest | 6 +++--- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index b6ea97506f0..62c35b276a3 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -1,13 +1,13 @@ import { REQUEST_TIMEOUT_MS } from "@/app/constant"; import { useAccessStore, useAppConfig, useChatStore } from "@/app/store"; -import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api"; -import Locale from "../../locales"; +import { prettyObject } from "@/app/utils/format"; import { EventStreamContentType, fetchEventSource, } from "@microsoft/fetch-event-source"; -import { prettyObject } from "@/app/utils/format"; +import Locale from "../../locales"; +import { ChatOptions, getHeaders, LLMApi, LLMUsage } from "../api"; export class ChatGPTApi implements LLMApi { public ChatPath = "v1/chat/completions"; @@ -15,7 +15,7 @@ export class ChatGPTApi implements LLMApi { public SubsPath = "dashboard/billing/subscription"; path(path: string): string { - let openaiUrl = useAccessStore.getState().openaiUrl; + let openaiUrl = useAccessStore.getState().openaiUrl(); if (openaiUrl.endsWith("/")) { openaiUrl = openaiUrl.slice(0, openaiUrl.length - 1); } diff --git a/app/components/home.tsx b/app/components/home.tsx index 810c9fa12c8..6d2393f29cb 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -2,7 +2,7 @@ require("../polyfill"); -import { useState, useEffect } from "react"; +import { useEffect, useState } from "react"; import styles from "./home.module.scss"; @@ -16,13 +16,13 @@ import { Path, SlotID } from "../constant"; import { ErrorBoundary } from "./error"; import { + Route, HashRouter as Router, Routes, - Route, useLocation, } from "react-router-dom"; -import { SideBar } from "./sidebar"; import { useAppConfig } from "../store/config"; +import { SideBar } from "./sidebar"; export function Loading(props: { noLogo?: boolean }) { return ( @@ -94,7 +94,7 @@ const loadAsyncGoogleFont = () => { const linkEl = document.createElement("link"); linkEl.rel = "stylesheet"; linkEl.href = - "/google-fonts/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap"; + "./google-fonts/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap"; document.head.appendChild(linkEl); }; diff --git a/app/layout.tsx b/app/layout.tsx index 37f5a9f1437..9c6405da097 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,8 +1,8 @@ /* eslint-disable @next/next/no-page-custom-font */ +import { getBuildConfig } from "./config/build"; import "./styles/globals.scss"; -import "./styles/markdown.scss"; import "./styles/highlight.scss"; -import { getBuildConfig } from "./config/build"; +import "./styles/markdown.scss"; const buildConfig = getBuildConfig(); @@ -33,8 +33,9 @@ export default function RootLayout({ - - + + + {children} diff --git a/app/locales/cn.ts b/app/locales/cn.ts index 032df3ab030..a384b55fb49 100644 --- a/app/locales/cn.ts +++ b/app/locales/cn.ts @@ -4,7 +4,7 @@ const cn = { WIP: "该功能仍在开发中……", Error: { Unauthorized: - "访问密码不正确或为空,请前往[设置](/#/settings)页输入正确的访问密码,或者填入你自己的 OpenAI API Key。", + "访问密码不正确或为空,请前往设置页输入正确的访问密码,或在设置页填入可用的 OpenAI API Key。", }, ChatItem: { ChatItemCount: (count: number) => `${count} 条对话`, diff --git a/app/store/access.ts b/app/store/access.ts index 91049846b1d..04cb2a5440c 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -1,7 +1,7 @@ import { create } from "zustand"; import { persist } from "zustand/middleware"; -import { StoreKey } from "../constant"; import { getHeaders } from "../client/api"; +import { StoreKey } from "../constant"; import { BOT_HELLO } from "./chat"; import { ALL_MODELS } from "./config"; @@ -11,8 +11,9 @@ export interface AccessControlStore { needCode: boolean; hideUserApiKey: boolean; - openaiUrl: string; + openaiUrl: () => string; + configUrl: () => string; updateToken: (_: string) => void; updateCode: (_: string) => void; enabledAccessControl: () => boolean; @@ -29,8 +30,13 @@ export const useAccessStore = create()( accessCode: "", needCode: true, hideUserApiKey: false, - openaiUrl: "/api/openai/", + openaiUrl() { + return window.location.pathname + "api/openai/"; + }, + configUrl() { + return window.location.pathname + "api/config"; // DO NOT add additional slash + }, enabledAccessControl() { get().fetch(); @@ -53,7 +59,7 @@ export const useAccessStore = create()( fetch() { if (fetchState > 0) return; fetchState = 1; - fetch("/api/config", { + fetch(this.configUrl(), { method: "post", body: null, headers: { diff --git a/next.config.mjs b/next.config.mjs index da23fd21b62..d1da8929cb5 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,6 +1,7 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + assetPrefix: './', experimental: { appDir: true, }, diff --git a/public/site.webmanifest b/public/site.webmanifest index 117f33b864e..4b91cb97bff 100644 --- a/public/site.webmanifest +++ b/public/site.webmanifest @@ -3,17 +3,17 @@ "short_name": "ChatGPT", "icons": [ { - "src": "/android-chrome-192x192.png", + "src": "./android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" }, { - "src": "/android-chrome-512x512.png", + "src": "./android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" } ], - "start_url": "/", + "start_url": "./", "theme_color": "#ffffff", "background_color": "#ffffff", "display": "standalone" From ccd488f96f13567e3a18e69866707a6f4286f0cd Mon Sep 17 00:00:00 2001 From: Limc Date: Wed, 17 May 2023 22:42:02 +0800 Subject: [PATCH 2/2] fix: bug --- public/serviceWorkerRegister.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/serviceWorkerRegister.js b/public/serviceWorkerRegister.js index 8405f21aaab..7ed2f691c8f 100644 --- a/public/serviceWorkerRegister.js +++ b/public/serviceWorkerRegister.js @@ -1,6 +1,6 @@ if ('serviceWorker' in navigator) { window.addEventListener('load', function () { - navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) { + navigator.serviceWorker.register('./serviceWorker.js').then(function (registration) { console.log('ServiceWorker registration successful with scope: ', registration.scope); }, function (err) { console.error('ServiceWorker registration failed: ', err);