File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1818 */
1919
2020import 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 = / ^ h t t p s ? : \/ \/ / 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
3923export default function robots ( ) : MetadataRoute . Robots {
4024 return {
Original file line number Diff line number Diff line change 2121import type { MetadataRoute } from "next" ;
2222import { source } from "@/lib/source" ;
2323import 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 = / ^ h t t p s ? : \/ \/ / i. test ( url ) ? url : `https://${ url } ` ;
239- return withProto . replace ( / \/ + $ / , "" ) ;
240- }
Original file line number Diff line number Diff line change 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 = / ^ h t t p s ? : \/ \/ / 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 ) ;
You can’t perform that action at this time.
0 commit comments