Skip to content

Commit 41f253f

Browse files
committed
feat: 使用本地字体替代 Google Fonts
- 新增 Noto Sans 本地字体文件(woff2 格式) - 使用 next/font/local 配置字体加载 - 移除运行时 Google Fonts 动态加载 - 移除 next.config.mjs 中的 Google Fonts 代理
1 parent 7fa2720 commit 41f253f

File tree

8 files changed

+30
-42
lines changed

8 files changed

+30
-42
lines changed

app/components/home.tsx

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,6 @@ const useHasHydrated = () => {
159159
return hasHydrated;
160160
};
161161

162-
const loadAsyncGoogleFont = () => {
163-
const linkEl = document.createElement("link");
164-
const proxyFontUrl = "/google-fonts";
165-
const remoteFontUrl = "https://fonts.googleapis.com";
166-
const googleFontUrl =
167-
getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
168-
linkEl.rel = "stylesheet";
169-
linkEl.href =
170-
googleFontUrl +
171-
"/css2?family=" +
172-
encodeURIComponent("Noto Sans:wght@300;400;700;900") +
173-
"&display=swap";
174-
document.head.appendChild(linkEl);
175-
};
176-
177162
function Screen() {
178163
const config = useAppConfig();
179164
const location = useLocation();
@@ -185,27 +170,6 @@ function Screen() {
185170
const shouldTightBorder =
186171
getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
187172

188-
// useEffect(() => {
189-
// loadAsyncGoogleFont();
190-
// }, []);
191-
useEffect(() => {
192-
const load = () => {
193-
try {
194-
loadAsyncGoogleFont();
195-
} catch {}
196-
};
197-
// 空闲时加载,低端网络或省流量模式直接跳过
198-
const conn = (navigator as any).connection;
199-
if (conn?.saveData || conn?.effectiveType === "2g") return;
200-
if ("requestIdleCallback" in window) {
201-
const id = (window as any).requestIdleCallback(load, { timeout: 2000 });
202-
return () => (window as any).cancelIdleCallback?.(id);
203-
} else {
204-
const t = setTimeout(load, 1200);
205-
return () => clearTimeout(t);
206-
}
207-
}, []);
208-
209173
if (isArtifact) {
210174
return (
211175
<Routes>

app/fonts/NotoSans-Black.woff2

13.1 KB
Binary file not shown.

app/fonts/NotoSans-Bold.woff2

13.1 KB
Binary file not shown.

app/fonts/NotoSans-Light.woff2

13.1 KB
Binary file not shown.

app/fonts/NotoSans-Regular.woff2

12.8 KB
Binary file not shown.

app/layout.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ import { Metadata, Viewport } from "next";
77
import { SpeedInsights } from "@vercel/speed-insights/next";
88
import { getServerSideConfig, getSidebarConfig } from "./config/server";
99
import { GoogleTagManager } from "@next/third-parties/google";
10+
import localFont from "next/font/local";
11+
12+
const notoSans = localFont({
13+
src: [
14+
{
15+
path: "./fonts/NotoSans-Light.woff2",
16+
weight: "300",
17+
style: "normal",
18+
},
19+
{
20+
path: "./fonts/NotoSans-Regular.woff2",
21+
weight: "400",
22+
style: "normal",
23+
},
24+
{
25+
path: "./fonts/NotoSans-Bold.woff2",
26+
weight: "700",
27+
style: "normal",
28+
},
29+
{
30+
path: "./fonts/NotoSans-Black.woff2",
31+
weight: "900",
32+
style: "normal",
33+
},
34+
],
35+
variable: "--font-noto-sans",
36+
display: "swap",
37+
});
1038

1139
const serverConfig = getServerSideConfig();
1240
const siderbarConfig = getSidebarConfig();
@@ -35,7 +63,7 @@ export default function RootLayout({
3563
children: React.ReactNode;
3664
}) {
3765
return (
38-
<html lang="en">
66+
<html lang="en" className={notoSans.variable}>
3967
<head>
4068
<meta name="config" content={JSON.stringify(getClientConfig())} />
4169
<meta

app/styles/globals.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
html {
107107
height: var(--full-height);
108108

109-
font-family: "Cascadia Code", "Consolas", "Noto Sans", "Microsoft YaHei", "SF Pro SC", "SF Pro Text", "SF Pro Icons",
109+
font-family: var(--font-noto-sans), "Cascadia Code", "Consolas", "Noto Sans", "Microsoft YaHei", "SF Pro SC", "SF Pro Text", "SF Pro Icons",
110110
"PingFang SC", "Helvetica Neue", "Helvetica", "Arial", sans-serif;
111111
}
112112

next.config.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ if (mode !== "export") {
8181
source: "/api/proxy/anthropic/:path*",
8282
destination: "https://api.anthropic.com/:path*",
8383
},
84-
{
85-
source: "/google-fonts/:path*",
86-
destination: "https://fonts.googleapis.com/:path*",
87-
},
8884
{
8985
source: "/sharegpt",
9086
destination: "https://sharegpt.com/api/conversations",

0 commit comments

Comments
 (0)