Skip to content

Commit acb3d7a

Browse files
longsizhuogithub-actions[bot]
authored andcommitted
refactor(site-url): 抽 normalizeSiteUrl 到 lib/site-url.ts,robots/sitemap 共用
app/robots.ts 和 app/sitemap.ts 各自维护了一份同形的 normalizeSiteUrl + RAW_SITE_URL + SITE_URL, 任何规范化调整都得改两处,容易 drift。 抽到 lib/site-url.ts:export normalizeSiteUrl() + export SITE_URL(模块加载时算一次), 两个消费方只 import { SITE_URL },删掉本地副本。 行为 byte-identical:默认 fallback 还是 'https://involutionhell.com', 归一化规则(无协议补 https://、去尾部所有斜杠)和正则完全不变,sitemap.ts 输出值不变。
1 parent b0a5a53 commit acb3d7a

3 files changed

Lines changed: 45 additions & 40 deletions

File tree

app/robots.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,7 @@
1818
*/
1919

2020
import type { MetadataRoute } from "next";
21-
22-
/**
23-
* 与 app/sitemap.ts 保持同源的站点根 URL。
24-
* 默认 fallback 到生产域名。
25-
*/
26-
const RAW_SITE_URL =
27-
process.env.NEXT_PUBLIC_SITE_URL ?? "https://involutionhell.com";
28-
29-
/**
30-
* 规范化:确保带协议头、去掉尾部斜杠。
31-
*/
32-
function normalizeSiteUrl(url: string): string {
33-
const withProto = /^https?:\/\//i.test(url) ? url : `https://${url}`;
34-
return withProto.replace(/\/+$/, "");
35-
}
36-
37-
const SITE_URL = normalizeSiteUrl(RAW_SITE_URL);
21+
import { SITE_URL } from "@/lib/site-url";
3822

3923
export default function robots(): MetadataRoute.Robots {
4024
return {

app/sitemap.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,9 @@
2121
import type { MetadataRoute } from "next";
2222
import { source } from "@/lib/source";
2323
import leaderboard from "@/generated/site-leaderboard.json";
24-
25-
/**
26-
* 从环境变量中读取的站点根 URL。
27-
* 默认为一个回退地址。
28-
*/
29-
const RAW_SITE_URL =
30-
process.env.NEXT_PUBLIC_SITE_URL ?? "https://involutionhell.com";
31-
32-
/**
33-
* 经过规范化处理的站点 URL(确保有协议头,且不带尾部斜杠)。
34-
* 例如: "https://example.com"
35-
*/
36-
const SITE_URL = normalizeSiteUrl(RAW_SITE_URL);
24+
// SITE_URL 由 lib/site-url.ts 统一提供(从 NEXT_PUBLIC_SITE_URL 读 + 归一化),
25+
// 这里和 app/robots.ts 共用一份,避免两边 drift。
26+
import { SITE_URL } from "@/lib/site-url";
3727

3828
/** * 定义 `source.getPages()` 返回的单个页面对象的类型别名
3929
*/
@@ -228,13 +218,3 @@ function isDraftOrHidden(page: SourcePage): boolean {
228218
d.frontmatter?.hidden
229219
);
230220
}
231-
232-
/**
233-
* 规范化站点的 URL。
234-
* * @param {string} url - 原始 URL 字符串。
235-
* @returns {string} 规范化后的 URL。
236-
*/
237-
function normalizeSiteUrl(url: string): string {
238-
const withProto = /^https?:\/\//i.test(url) ? url : `https://${url}`;
239-
return withProto.replace(/\/+$/, "");
240-
}

lib/site-url.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// lib/site-url.ts
2+
3+
/**
4+
* @file lib/site-url.ts
5+
* @description
6+
* 站点根 URL 的 single source of truth。
7+
*
8+
* 之前 `app/sitemap.ts` 和 `app/robots.ts` 各自维护了一份 `normalizeSiteUrl` +
9+
* `RAW_SITE_URL` / `SITE_URL` 逻辑,完全同形。抽到这里避免两边独立 drift —— 任何
10+
* 调整(比如以后加 trailing-slash 归一、加端口清洗、换 env 名)只改这一个地方。
11+
*
12+
* 注意:保留与原实现完全一致的语义,只换引用来源,不变值。sitemap.ts 的单元不变性
13+
* 依赖这一点。
14+
*/
15+
16+
/**
17+
* 原始 env 值,fallback 到生产域名。
18+
* 不直接 export —— 消费方只应该拿规范化后的 SITE_URL。
19+
*/
20+
const RAW_SITE_URL =
21+
process.env.NEXT_PUBLIC_SITE_URL ?? "https://involutionhell.com";
22+
23+
/**
24+
* 规范化站点 URL:
25+
* 1. 没协议头的补 `https://`
26+
* 2. 去掉任意数量的尾部斜杠
27+
*
28+
* 例:
29+
* "example.com" → "https://example.com"
30+
* "https://example.com/" → "https://example.com"
31+
* "https://example.com//" → "https://example.com"
32+
*/
33+
export function normalizeSiteUrl(url: string): string {
34+
const withProto = /^https?:\/\//i.test(url) ? url : `https://${url}`;
35+
return withProto.replace(/\/+$/, "");
36+
}
37+
38+
/**
39+
* 模块加载时计算一次,给 robots / sitemap / 其它需要站点根的地方直接 import 用。
40+
*/
41+
export const SITE_URL = normalizeSiteUrl(RAW_SITE_URL);

0 commit comments

Comments
 (0)