fix: app routing

This commit is contained in:
2026-02-26 14:10:16 +08:00
parent b5fbdc17d2
commit 34c2f9e346
3 changed files with 35 additions and 39 deletions
+10 -13
View File
@@ -2,13 +2,6 @@ let dataIndex: Record<string, string> | null = null;
let isDevIndexLoaded = false;
let isCliIndexLoaded = false;
/**
* 设置全局数据索引(CLI 环境使用)
*/
export function setDataIndex(index: Record<string, string>) {
dataIndex = index;
}
/**
* 从索引获取数据
*/
@@ -27,13 +20,16 @@ async function loadDevIndex(): Promise<void> {
isDevIndexLoaded = true;
// @ts-ignore - import.meta.glob 在构建时会被处理
if (typeof import.meta !== 'undefined' && import.meta.glob) {
if (typeof import.meta !== "undefined" && import.meta.glob) {
try {
// @ts-ignore - 只加载 .csv 和 .md 文件
const modules = import.meta.glob('../../content/**/*.{csv,md}', { as: 'raw', eager: true });
const modules = import.meta.glob("../../content/**/*.{csv,md}", {
as: "raw",
eager: true,
});
const index: Record<string, string> = {};
for (const [path, content] of Object.entries(modules)) {
const normalizedPath = path.replace('/src/', '/');
const normalizedPath = path.replace("/src/", "/");
index[normalizedPath] = content as string;
}
dataIndex = { ...dataIndex, ...index };
@@ -51,9 +47,9 @@ async function loadCliIndex(): Promise<void> {
isCliIndexLoaded = true;
try {
const response = await fetch('/__CONTENT_INDEX.json');
const response = await fetch("/__CONTENT_INDEX.json");
if (!response.ok) {
throw new Error('Failed to fetch content index');
throw new Error("Failed to fetch content index");
}
const index = await response.json();
dataIndex = { ...dataIndex, ...index };
@@ -81,6 +77,7 @@ export async function fetchData(path: string): Promise<string> {
}
// 索引不存在时,使用 fetch 加载
if (dataIndex !== null) throw new Error(`no data in index: ${path}`);
try {
const response = await fetch(path);
if (!response.ok) {
@@ -88,7 +85,7 @@ export async function fetchData(path: string): Promise<string> {
}
return await response.text();
} catch (error) {
console.error('fetchData error:', error);
console.error("fetchData error:", error);
throw error;
}
}