diff --git a/vite.config.mjs b/vite.config.mjs index 1f3cee3..cf4f3e0 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -103,9 +103,15 @@ const localFileStoragePlugin = () => { const filePath = path.join(dataDir, `${safeKey}.json`); if (fs.existsSync(filePath)) { - const data = fs.readFileSync(filePath, 'utf-8'); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ success: true, data: JSON.parse(data) })); + try { + // 文件可能带 UTF-8 BOM(外部编辑器/同步工具写入),解析前需剥离,否则 JSON.parse 抛错 + const raw = fs.readFileSync(filePath, 'utf-8').replace(/^\uFEFF/, ''); + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ success: true, data: JSON.parse(raw) })); + } catch (e) { + res.statusCode = 500; + res.end(JSON.stringify({ success: false, error: e.message })); + } } else { res.statusCode = 404; res.end(JSON.stringify({ success: false, error: 'Not found' }));